반응형
Jasmine의 toHaveBeenCalledWith 메서드와 함께 객체 유형 사용
방금 Jasmine을 사용하기 시작 했으므로 초보자 질문을 용서하십시오.하지만 사용할 때 개체 유형을 테스트 할 수 toHaveBeenCalledWith
있습니까?
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.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')
반응형
'your programing' 카테고리의 다른 글
Rails 3에서 최신 기록 찾기 (0) | 2021.01.05 |
---|---|
MySQL에 연결하고 쿼리를 실행하는 데 가장 안전한 Python 모범 사례 (0) | 2021.01.05 |
빌드 타임 스탬프를 APK에 쓰는 방법 (0) | 2020.12.31 |
나뭇 가지 함수에서 절대 경로를 사용하는 방법 (0) | 2020.12.31 |
EditText를 만드는 방법은 Android에서만 알파벳을 허용합니까? (0) | 2020.12.31 |