And*_*rew 3 javascript tdd jasmine
是否有可能创建一个对象obj的模拟,使得Jasmine测试就像
expect(fakeB instanceof B).toBe(true);
Run Code Online (Sandbox Code Playgroud)
经过?
换句话说,我有一个带有方法convertToB的A类,该参数必须是B类的一个实例:
function A(){
this.convertToB = function(elem){
if (!(elem instanceof B)){ throw an error}
...
...
}
}
Run Code Online (Sandbox Code Playgroud)
我想通过创建一个模拟对象来测试该段代码,当被问到它是否是B的实例时它会响应为真.
目前我被迫写了一些测试
这有点烦人.我期待像这样的命令
var fakeB = jasmine.createFake('B')
Run Code Online (Sandbox Code Playgroud)
这样这个问题的第一行代码就会通过.
小智 5
我的代码中有几十个地方和你的一样.spyOn方法的Jasmine 2.0将完成这项工作.与Jasmine 1.0相同,但我不记得该方法是否以完全相同的方式调用/使用.举个例子:
var realB = new B();
// change realB instance into a spy and mock its method 'foo' behaviour to always return 'bar'
// it will still respond "true" to "realB instanceof B"
spyOn(realB, 'foo').and.returnValue('bar')
var realC = new C();
// C.baz is expecting instance of B to be passed as first argument
var result = C.baz(realB)
// assuming C.baz return realB.foo() concatenated with '123'
expect(result).toEqual('bar123');
Run Code Online (Sandbox Code Playgroud)
Jasmine文档中有大量间谍示例:http://jasmine.github.io/2.0/introduction.html
| 归档时间: |
|
| 查看次数: |
10607 次 |
| 最近记录: |