我想模拟这样的对象:
var target = new Y.Mock(),
eventObject = {};
Y.Mock.expect(target, {
method: 'get',
args: ['actionId'],
returns: 'edit'
});
Y.Mock.expect(target, {
method: 'get',
args: ['container'],
returns: '<div></div>'
});
eventObject.target = target;
但是我收到错误:
Argument 0 of get() is incorrect.
Expected: container (string)
Actual: actionId (string)"
如何避免这种情况?
最佳答案
简单看一下Y.Mock code代码,在我看来,您不能使用相同的参数对同一方法创建两个期望。后者覆盖第一个。
但是没关系,我们可以自己做一些 mock :
var mockedGet = function (args) {
if (args === 'container') {
return '<div></div>';
} else if (args === 'actionId') {
return 'edit';
} else {
YUITest.Assert.fail('Method get('+args+') should not have been called.');
}
// we could also verify call counts etc, using the mock object as 'this'
}
Y.Mock.expect(target, {
method: 'get',
args: [YUITest.Mock.Value.String], // accepts only one String
run: mockedGet // replaces returns
});
关于javascript - 如何测试YUI3的get()方法返回的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25259485/