用Jasmine的toHaveBeenCalledWith方法使用对象types

我刚开始使用茉莉花,所以请原谅新手问题,但有可能使用toHaveBeenCalledWithtesting对象types?

 expect(object.method).toHaveBeenCalledWith(instanceof String); 

我知道我可以这样做,但它是检查返回值而不是参数。

 expect(k instanceof namespace.Klass).toBeTruthy(); 

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.mostRecentCall.args[0] instanceof String).toBeTruthy(); }); }); 

我发现了一个更酷的机制,使用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')