javascript - How to check if object contains an item using Jasmine -
i using karma , jasmine testing framework. code:
it('add() should add x reply object', function() { spyon(ctrl, 'addxreply'); ctrl.reply = {}; ctrl.reply.post = 'test post'; ctrl.add(); expect(ctrl.addxreply).tohavebeencalled(); console.log(ctrl.reply); expect(ctrl.reply).tocontain('x'); }); this ctrl.add():
self.add = function() { self.reply['x'] = self.posts[0].id; self.addxreply(); }; the problem is, when run code, returns:
log: object{post: 'test post', x: undefined} chromium 48.0.2564 (ubuntu 0.0.0) controller: mainctrl add() should add x reply object failed expected object({ post: 'test post', x: undefined }) contain 'x'. as can see, reply object contain x line expect(ctrl.reply).tocontain('x'); still failing. idea how can verify object contains x?
you have bug in created vs what's expected. notice line:
self.reply['x'] = self.posts[0].id; it expects ctrl have property "posts" array has index 0 has property named id. every 1 of conditions fails
you instead defined singular property (not array) under ctrl's property reply:
ctrl.reply.post you need change test code:
it('add() should add x reply object', function() { spyon(ctrl, 'addxreply'); ctrl.reply = {}; //ctrl needs array named "posts" 1 index //containing object "id" property ctrl.posts = [ { "id": 'test post' } ]; ctrl.add(); expect(ctrl.addxreply).tohavebeencalled(); console.log(ctrl.reply); expect(ctrl.reply).tocontain('x'); });
Comments
Post a Comment