jsMockito方法调用断言未按预期工作

发布于 2024-11-09 09:50:41 字数 991 浏览 0 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈独秀 2024-11-16 09:50:41

断言正在检查对象本身,而不是其属性,并且 JsHamcrest 'equalTo' 匹配器不会对对象进行深入检查(它与 javascript '==' 运算符相同)。

您可以简单地检查它是否是一个对象:

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

或者有一个 JsHamcrest 匹配器“hasMember”,您可以将其用作:

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

如果您使用的是 jshamcrest 0.6.4 或更高版本(或当前的 HEAD),那么您还可以验证通过将匹配器作为第二个参数传递给 hasMember 来获取成员:

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

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')));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文