2016-06-09 53 views
2

當試圖使用$resource測試一些簡單的角度代碼,我結束了一個Resource對象含有$promise鍵,並因此具有以下形式的失敗:Failure/Error: Expected Resource(...) to equal Object(...)測量角-資源:期望的對象,得到了一個資源

我期待回到我作爲httpBackend.expectGET('/books/5.json').respond(my_book)的一部分傳遞給respond方法的對象。我在使用$resource錯誤還是在測試中出了問題?

代碼

var bookApp = angular.module('bookApp', 
    [ 
    'ngResource', 
    ] 
); 

function BookController(scope, $resource) { 
    var ctrl = this; 
    var Book = $resource('/books/:selected.json', {selected:'@id'}); 

    ctrl.fetch_book = function(id){ 
    console.log('Selecting options ' + id); 
    Book.get({selected:id}, function(data){ 
     console.log('Received: ' + JSON.stringify(data)); 
     ctrl.current_book = data; 
    }); 
    }; 
} 

BookController.$inject = ['$scope', '$resource']; 

bookApp.component('book', { 
    controller: BookController 
}); 

測試

describe('component: tree', function() { 
    var component_controller, $componentController, httpBackend, my_book; 

    beforeEach(module('bookApp')); 

    beforeEach(inject(function($httpBackend, _$componentController_) { 
    httpBackend = $httpBackend; 
    $componentController = _$componentController_; 
    })); 

    describe('$ctrl.fetch_book(book_id)', function(){ 

    beforeEach(function() { 
     component_controller = $componentController('book'); 
     my_book = {title: 'Sanctuary', id: '5'}; 
    }); 

    it('fetches the book with id=book_id', function(){ 
     httpBackend.expectGET('/books/5.json').respond(my_book); 
     component_controller.fetch_book(5); 
     httpBackend.flush(); 
     console.log('Options: ' + JSON.stringify(component_controller.current_book)); 
     console.log('constructor: ' + JSON.stringify(component_controller.current_book.constructor.name)); 
     expect(component_controller.current_book).toEqual(my_book); 
    }); 
    }); 
}); 

結果

$ bundle exec teaspoon -f documentation 

component: tree 
    $ctrl.fetch_book(book_id) 
    fetches the book with id=book_id (FAILED - 1) 
     # Selecting options 5 
     # Received: {"title":"Sanctuary","id":"5"} 
     # Options: {"title":"Sanctuary","id":"5"} 
     # constructor: "Resource" 

Failures: 

    1) component: tree $ctrl.fetch_book(book_id) fetches the book with id=book_id 
    Failure/Error: Expected Resource({ title: 'Sanctuary', id: '5', 
    $promise: Promise({ $$state: Object({ status: 1, value: 
    <circular reference: Object> }) }), $resolved: true }) to equal 
    Object({ title: 'Sanctuary', id: '5' }). 

Finished in 0.02600 seconds 
1 example, 1 failure 

回答

1

在測試儀試試這個:

expect(component_controller.current_book).toEqual(angular.toJSON(my_book)); 

它會剝離對象的屬性,並且您將會匹配。

您也可以嘗試angular.equals但我沒有測試過。

1

嘗試添加以下內容到您的spec文件中,看看它是否有效。我在PhoneCat示例中看到它,它對我很有用。

beforeEach(function() { 
    jasmine.addCustomEqualityTester(angular.equals); 
    }); 
0

你可以嘗試做這樣的事情:

我在那裏我得到

預期目標的錯誤是一個類型的對象同樣的問題,但資源(

這就是我之前的:

我加.toJSON
expect(self.project).toEqual(mockProject); 

後(),它是所有好:

expect(self.project.toJSON()).toEqual(mockProject); 

希望這有助於!