javascript - Expected null({ }) to equal Object({ }) -
i have been trying run angularjs own unit tests of $resource. difference i'm running jasmine v2.0 , karma v0.13.
did work of converting custom actions older jasmine newer, tests pass. all...
i have stumbled 1 type of tests. believe have $httpbackend
. whilst testing this line see fails:
expected null({ }) equal object({ }).
actual test code, problem latest expectation:
// --- 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
found it.
turns out, angular uses function bellow create empty object. newest versions of jasmine fails on calling objects instantiated null
, {}
equals.
/** * creates new object without prototype. object useful lookup without having * 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); }
after looking @ angularjs source, see objects created using object.create(null)
, in snippet below.
/** * creates new object without prototype. object useful lookup without having * 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); }
looks jasmine 2.0 added comparison prototype type well, expectation fails.
test can written this: expect(callback.calls.mostrecent().args[1]()).toequal(object.create(null));
, pass.
Comments
Post a Comment