Jasmine-생성자 내에서 메서드 호출 감시
Javascript 객체 생성자에서 다음 메서드가 호출되는지 테스트하고 싶습니다. Jasmine 문서에서 본 내용에서 생성자 메서드를 감시 할 수 있고 객체가 인스턴스화 된 후에 메서드를 감시 할 수 있지만 객체가 생성되기 전에 메서드를 감시 할 수없는 것 같습니다.
목적:
Klass = function() {
this.called_method();
};
Klass.prototype.called_method = function() {
//method to be called in the constructor.
}
사양에서 다음과 같이하고 싶습니다.
it('should spy on a method call within the constructor', function() {
spyOn(window, 'Klass');
var obj = new Klass();
expect(window.Klass.called_method).toHaveBeenCalled();
});
프로토 타입 메서드를 직접 스파이 :
describe("The Klass constructor", function() {
it("should call its prototype's called_method", function() {
spyOn(Klass.prototype, 'called_method'); //.andCallThrough();
var k = new Klass();
expect(Klass.prototype.called_method).toHaveBeenCalled();
});
});
전반적으로 나는 위의 Dave Newton의 답변에 동의합니다. 그러나이 접근 방식에는 고려해야 할 몇 가지 예외적 인 경우가 있습니다.
다른 테스트 사례를 사용하여 Dave의 솔루션을 변형합니다.
// production code
var Klass = function() {
this.call_count = 0;
this.called_method();
};
Klass.prototype.called_method = function() {
++this.call_count;
};
// test code
describe("The Klass constructor", function() {
it("should call its prototype's called_method", function() {
spyOn(Klass.prototype, 'called_method');
var k = new Klass();
expect(k.called_method).toHaveBeenCalled();
});
it('some other test', function() {
var k = new Klass();
expect(k.call_count).toEqual(1);
});
});
The second test will fail because the spy setup in the first test persists across the test boundaries into the second method; called_method doesn't increment call_count, so this.call_count does not equal 1. It's also possible to come up with scenarios with false positives - tests that pass, that shouldn't.
On top of this, because the spy remains, the more Klass instances that are created, the bigger the memory heap the spy will consume, because the spy will record each call to called_method. This probably isn't a problem in most circumstances, but you should be aware of it, just in case.
A simple solution to this problem would be to make sure that the spy is removed after it has been used. It can look a bit ugly, but something like this works:
// test code
describe("The Klass constructor", function() {
it("should call its prototype's called_method", function() {
var spy = jasmine.createSpy('called_method');
var method = Klass.prototype.called_method;
Klass.prototype.called_method = spy;
var k = new Klass();
expect(spy).toHaveBeenCalled();
Klass.prototype.called_method = method;
});
[NOTE - a little opinion to finish] A better solution would be to change the way you write production code to make the code easier to test. As a rule, spying on prototypes is probably a code-smell to be avoided. Instead of instantiating dependencies in the constructor, inject them. Instead of doing initialization in the constructor, defer to an appropriate init method.
참고URL : https://stackoverflow.com/questions/8733978/jasmine-spying-on-a-method-call-within-a-constructor
'IT TIP' 카테고리의 다른 글
PHP의 엄격한 모드? (0) | 2020.11.30 |
---|---|
Web.config 변환 옵션이 회색으로 표시됩니다. (0) | 2020.11.30 |
IOException이 Not-Enough-Disk-Space-Exception 유형인지 확인하는 방법은 무엇입니까? (0) | 2020.11.30 |
Jenkins 사용자 인증 세부 정보를 Jenkins API를 사용하여 작업을 생성하는 스크립트에 어떻게 "전달"할 수 있습니까? (0) | 2020.11.30 |
AngularJS 드롭 다운 필수 유효성 검사 (0) | 2020.11.30 |