开发者

jsMockito method invocation assertion not working as expected

I'm having trouble making some method invocation assertions when testing a Javascript Object with QUnit + JsMockito. The basic infrastructure is working ok: Qunit, JsHamcrest(Jsmockito dependency) and Jsmockito are properly initialized at the suite definition.

I want to assert a call to "dataStore#create" method with an object as an argument, as follows:

var store = {create: function(arg) {}};

test("save()", function() {
  var dataStoreMock = mock(store);

  var objectUnderTest = { value: 'aaa',
                        dataStore: dataStoreMock,
                        save: function() {this.dataStore.create({name: this.value});}}

objectUnderTest.save();
verify(dataStoreMock).create({name: 'aaa'});
});

I get the assertion error: "Wanted but not invoked: obj.create(equal to [object Object])"

My first suspect is that object equality isn't working as i expected, the proof is that the assertion works when used on calls that passes primitive data types instead of objects:

this.dataStore.create(this.value); //actual code
verify(dataStoreMock).create('aaa'); //test

I tried to use jsHamcrest 'equalTo' matcher (a开发者_如何学编程s exposed at jsMockito docs) without success as well :

verify(dataStoreMock).create(equalTo({name: 'aaa'}));

Does anyone have any ideas on how to make this kind of assertion work?


The assertion is checking the object itself, not its attributes, and the JsHamcrest 'equalTo' matcher does not do deep inspection of objects (it is the same as the javascript '==' operator).

You could simply check that it is an object:

verify(dataStoreMock).create(object());

or there is a JsHamcrest matcher 'hasMember', which you could use as:

verify(dataStoreMock).create(hasMember('name'));

If you're using a jshamcrest 0.6.4 or later (or the current HEAD), then you can also verify the member by passing matchers as a second argument to hasMember:

verify(dataStoreMock).create(hasMember('name', 'aaa'));
verify(dataStoreMock).create(hasMember('name', equalTo('aaa')));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜