더북(TheBook)

콘텍스트까지 고려한 테스트를 작성하자(예제 2-36).

예제 2-36 올바른 콘텍스트에서 타깃이 실행되는지 테스트

소스 파일 2장\AOP\Aop_05_tests.js

it(‘타깃 함수를 해당 객체의 콘텍스트에서 실행한다’, function() {
var Target = function() {
  var self = this;
  this.targetFn = function() {
    expect(this).toBe(self);
  };
};
var targetInstance = new Target();
var spyOnInstance = spyOn(targetInstance,‘targetFn’).and.callThrough();
Aop.around(‘targetFn’,argPassingAdvice,targetInstance);
targetInstance.targetFn();
expect(spyOnInstance).toHaveBeenCalled();
});
 
it(‘어드바이스를 타깃의 콘텍스트에서 실행한다’, function() {
var advice = function() {
  expect(this).toBe(targetObj);
};
Aop.around(‘targetFn’,advice,targetObj);
targetObj.targetFn();
});

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.