scr*_*key 57 javascript tdd bdd jasmine
我刚刚开始使用Jasmine,请原谅新手问题但是在使用时是否可以测试对象类型toHaveBeenCalledWith?
expect(object.method).toHaveBeenCalledWith(instanceof String);
Run Code Online (Sandbox Code Playgroud)
我知道我可以做到这一点,但它正在检查返回值而不是参数.
expect(k instanceof namespace.Klass).toBeTruthy();
Run Code Online (Sandbox Code Playgroud)
Wol*_*old 101
我已经发现了一种更加冷却的机制,jasmine.any()因为我发现手动分开的参数对于易读性来说是次优的.
在CoffeeScript中:
obj = {}
obj.method = (arg1, arg2) ->
describe "callback", ->
it "should be called with 'world' as second argument", ->
spyOn(obj, 'method')
obj.method('hello', 'world')
expect(obj.method).toHaveBeenCalledWith(jasmine.any(String), 'world')
Run Code Online (Sandbox Code Playgroud)
And*_*rle 52
toHaveBeenCalledWith是一种间谍的方法.所以你只能在间谍上调用它们,如文档中所述:
// your class to test
var Klass = function () {
};
Klass.prototype.method = function (arg) {
return arg;
};
//the test
describe("spy behavior", function() {
it('should spy on an instance method of a Klass', function() {
// create a new instance
var obj = new Klass();
//spy on the method
spyOn(obj, 'method');
//call the method with some arguments
obj.method('foo argument');
//test the method was called with the arguments
expect(obj.method).toHaveBeenCalledWith('foo argument');
//test that the instance of the last called argument is string
expect(obj.method.calls.mostRecent().args[0] instanceof String).toBeTruthy();
});
});
Run Code Online (Sandbox Code Playgroud)