jsMockito方法调用断言未按预期工作
使用 QUnit + JsMockito 测试 Javascript 对象时,我在进行某些方法调用断言时遇到问题。基本基础设施工作正常:Qunit、JsHamcrest(Jsmockito 依赖项)和 Jsmockito 在套件定义中正确初始化。
我想断言对“dataStore#create”方法的调用,并以对象作为参数,如下所示:
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'});
});
我收到断言错误:“想要但未调用:obj.create(等于 [object Object])”
我的第一个怀疑是对象相等性没有按我的预期工作,证明是断言在传递原始数据类型而不是对象的调用上使用时有效:
this.dataStore.create(this.value); //actual code
verify(dataStoreMock).create('aaa'); //test
我尝试使用 jsHamcrest 'equalTo' 匹配器(如在 jsMockito 文档中公开)也没有成功:
verify(dataStoreMock).create(equalTo({name: 'aaa'}));
有人对如何使这种断言起作用有任何想法吗?
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 (as 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
断言正在检查对象本身,而不是其属性,并且 JsHamcrest 'equalTo' 匹配器不会对对象进行深入检查(它与 javascript '==' 运算符相同)。
您可以简单地检查它是否是一个对象:
或者有一个 JsHamcrest 匹配器“hasMember”,您可以将其用作:
如果您使用的是 jshamcrest 0.6.4 或更高版本(或当前的 HEAD),那么您还可以验证通过将匹配器作为第二个参数传递给 hasMember 来获取成员:
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:
or there is a JsHamcrest matcher 'hasMember', which you could use as:
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: