ジャスミンのtohavebeecalleded withメソッドを使用してオブジェクトタイプを使用します
-
25-10-2019 - |
質問
私はジャスミンの使用を始めたばかりなので、初心者の質問を許してくださいが、使用時にオブジェクトタイプをテストすることは可能ですか toHaveBeenCalledWith
?
expect(object.method).toHaveBeenCalledWith(instanceof String);
私はこれができることを知っていますが、それは議論ではなく返品値をチェックしています。
expect(k instanceof namespace.Klass).toBeTruthy();
解決
toHaveBeenCalledWith
スパイの方法です。したがって、Spyで説明したようなスパイでのみ呼び出すことができます ドキュメント:
// 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();
});
});
他のヒント
使用して、さらにクールなメカニズムを発見しました 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')
所属していません StackOverflow