Struggling with running AngularJS unit tests for $resource using Jasmine v2.0 and Karma v0.13, I managed to convert custom actions to newer syntax successfully. However, a specific type of test is giving me trouble, which I suspect is related to $httpBackend
. When testing this line, the following failure occurs:
Expected null({ }) to equal Object({ }).
The issue lies in the latest expectation in my actual test code:
// ---
callback = jasmine.createSpy();
// ---
it("should create resource", function() {
$httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123, name: 'misko'});
var cc = CreditCard.save({name: 'misko'}, callback);
expect(cc).toEqualData({name: 'misko'});
expect(callback).not.toHaveBeenCalled();
$httpBackend.flush();
expect(cc).toEqualData({id: 123, name: 'misko'});
expect(callback).toHaveBeenCalledOnce();
expect(callback.calls.mostRecent().args[0]).toEqual(cc);
expect(callback.calls.mostRecent().args[1]()).toEqual({});
});
UPDATE
Resolved the issue.
It appears that the problem stems from how Angular creates an empty object when comparing with null
and {}
. The newest versions of Jasmine seem to struggle with this comparison method.
/**
* Creates a new object without a prototype. This object is useful for lookup without having to
* guard against prototypically inherited properties via hasOwnProperty.
*
* Related micro-benchmarks:
* - http://jsperf.com/object-create2
* - http://jsperf.com/proto-map-lookup/2
* - http://jsperf.com/for-in-vs-object-keys2
*
* @returns {Object}
*/
function createMap() {
return Object.create(null);
}