toHaveBeenCalledWith
は、スパイの方法です。だから、docsで説明したようにあなたが唯一のスパイでそれらを呼び出すことができます。私は読みやすさのために、最適であることを手で離れて引数を取る見つけるよう
// 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();
});
});
アンドレアス、あなたは '.toBeTruthy()'を追加何らかの理由があるのでしょうか?それは不要だと思われます。 – gwg
マッチャーなしの@gwg 'expect(foo)'はノーオペレーションです。 'toBeTruthy()'コールがなければ何もしません。証明についてはhttp://jsfiddle.net/2doafezv/2/を参照してください。 –
これは古くなっています。 'obj.method.mostRecentCall'はJasmine 2.0の[obj.method.calls.mostRecent()'](http://jasmine.github.io/2.0/introduction.html#section-Other_tracking_properties)になる必要があります。また、 'jasmine.any()'を使用すると、他の答えに記載されているように、より明確でより綺麗です。最後に、この回答には少し時間がかかります。基本的にあなたが 'expect(obj.method.mostRecentCall.args [0] instanceof String).toBeTruthy();'のように書いたものは、あなた自身を説明するのに本当に必要なものではありません。 –