your programing

Jasmine의 toHaveBeenCalledWith 메서드와 함께 객체 유형 사용

lovepro 2021. 1. 5. 19:45
반응형

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')

참조 URL : https://stackoverflow.com/questions/8801060/using-object-types-with-jasmines-tohavebeencalledwith-method

반응형