gpt4 book ai didi

javascript - 如何测试YUI3的get()方法返回的值?

转载 作者:行者123 更新时间:2023-11-29 21:58:19 26 4
gpt4 key购买 nike

我想像这样模拟一个对象:

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代码,在我看来你不能用相同的参数在同一个方法上创建两个期望。后者覆盖第一个。

不过没关系,我们可以自嘲一下:

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
});

Working example .

关于javascript - 如何测试YUI3的get()方法返回的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25259485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com