2012-10-30 4 views
6

나는 거의 동일한 테스트를 많이하고 있습니다. DRY와 scanability의 관점에서 테스트를 하나의 함수로 추상화 한 다음 몇 가지 매개 변수로 함수를 호출하고 싶습니다. 그런 다음이 함수는 it을 호출하고 해당 사양을 제품군에 추가합니다.함수 안에 재스민 스펙을 정의 할 수 있나요? 그리고 beforeEach를 적용 할 수 있습니까?

사양이 다른 사양과 동일한 방식으로 실행되지 않고 beforeEach이 공용 기능에서 정의 된 사양보다 먼저 호출되지 않는다는 점을 제외하면 작동하는 것처럼 보입니다. 이 일을하고 beforeEachafterEach의 기능을 유지하는 방법이

define(['modules/MyModule','jasmine/jasmine'], function(MyModule) { 

    describe('myModule', function() { 

     function commonTests(params) { 
      it('should pass this test OK', function() { 
       expect(true).toBe(true); 
      }); 

      it('should fail because module is undefined', function() { 
       expect(module[params.method]()).toBe('whatever'); 
      }); 
     } 

     var module; 

     beforeEach(function() { 
      module = new MyModule(); 
     }); 

     describe('#function1', function() { 
      commonTests({ 
       method: 'function1' 
      }); 
     }); 

     describe('#function2', function() { 
      commonTests({ 
       method: 'function2' 
      }); 
     }); 

    }); 

}); 

있습니까?

UPDATE : 내 예를 잘못 가지고 같은

이 보이는, 죄송합니다.

define(['modules/MyModule'], function(MyModule) { 

    function commonTests(params) { 
     it('will fail because params.module is undefined', function() { 
      expect(typeof params.module).toBe('object'); 
      expect(typeof params.module[params.method]).toBe('function'); 
     }); 

     it('has a few tests in here', function() { 
      expect(true).toBe(true); 
     }); 
    } 


    describe('MyModule', function() { 

     var module; 

     beforeEach(function() { 
      module = new MyModule(); 
     }); 

     describe('#function1', function() { 
      commonTests({ 
       module: module, 
       method: 'function1' 
      }); 
     }); 

     describe('#function2', function() { 
      commonTests({ 
       module: module, 
       method: 'function2' 
      }); 
     }); 

    }); 
}); 

내가 module의 값이 항상 첫 번째 예에서와 같이 module의 현재 값을 사용하는 대신 commonTests에 호출의 일부로서 보존되어 있기 때문에 실패 생각 : 여기에 실패하는 경우입니다. 나는 거기에 도착하면 내 솔루션을 게시 할 것입니다 ...

답변

4

감사합니다! 내가 사용하고 최종 솔루션은 매우 유사합니다 :

define(['modules/MyModule'], function(MyModule) { 

    var module; 

    function commonTests(params) { 
     it('will not fail because module is shared', function() { 
      expect(typeof module).toBe('object'); 
      expect(typeof module[params.method]).toBe('function'); 
     }); 

     it('has a few tests in here', function() { 
      expect(true).toBe(true); 
     }); 
    } 


    describe('MyModule', function() { 

     beforeEach(function() { 
      module = new MyModule(); 
     }); 

     describe('#function1', function() { 
      commonTests({ 
       method: 'function1' 
      }); 
     }); 

     describe('#function2', function() { 
      commonTests({ 
       method: 'function2' 
      }); 
     }); 

    }); 
}); 

비록 당신은 당신이 약간 다른 접근을해야 commonTests에 인수로 module에 통과에 대해 서로 다른 기능을 할 수있는 능력을 가지고 필요한 경우 각 it 블록 :

define(['modules/MyModule'], function(MyModule) { 

    var module; 

    function commonTest1(params) { 
     expect(typeof module).toBe('object'); 
     expect(typeof module[params.method]).toBe('function'); 
    } 

    function commonTest2(params) { 
     expect(true).toBe(true); 
    } 


    describe('MyModule', function() { 

     beforeEach(function() { 
      module = new MyModule(); 
     }); 

     describe('#function1', function() { 

      it('will not fail because module is shared', function() { 
       commonTest1({ method: 'function1' }); 
      }); 

      it('has a few tests in here', function() { 
       commonTest2({ method: 'function1' }); 
      }); 

     }); 

     describe('#function2', function() { 

      it('will not fail because module is shared', function() { 
       commonTest1({ method: 'function2' }); 
      }); 

      it('has a few tests in here', function() { 
       commonTest2({ method: 'function2' }); 
      }); 

     }); 

    }); 
}); 

일반적인 테스트를 포함하는 함수의 실행이 beforeEach가 콜백을 실행 한 후까지 지연이 방법.

0

네,이 시나리오는 우리에게 잘 작동합니다. 우리는 gated checkins에서 테스트를 실행하기 위해 chutzpah를 사용하고 afterEach 함수 없이는 테스트가 실패합니다.

그러나 define 줄은 require.js처럼 보입니다. 우리는 그 문제에 몇 가지 문제점이있었습니다 ... MyModuleundefined이거나 예상대로 정의되지 않았다고 가정합니다. beforeEach 메서드에서 중단 점을 설정하여 해당 호출이 호출되었고 어떤 값이 MyModule에 있는지 확인 했습니까? 시험 전화는 어떻게하고 있니?

define 행없이 코드를 실행하면 beforeEach 함수는 예상대로 4x 호출됩니다. Btw 나는 chutzpah.console.exe를 사용하여 테스트를 호출합니다.

편집 :

그래, 이제 내 PC에서 문제를 재현 할 수 있습니다. 그리고 나에게 훨씬 더 분명합니다. 처음에는 모든 변수가 먼저 정의되지 않으므로 module은 정의되지 않습니다. 그런 다음 beforeEachdescribe을 사용하여 재스민에 기능을 등록하십시오. 재스민은

function() { 
    commonTests({ 
     module: module, 
     method: 'function1' 
    }); 
} 

으로 시작하여 describe로 전달되었습니다. 라인 module: module에서 참조var module을 새 오브젝트 특성 module에 복사하고이 내용은 지금까지 undefined을 참조합니다. beforeEach 콜백 ... 올바른 값하지만 잘못된 참조가 이미 복사 된에 var module의 기준을 변경 호출 될 때

나는 function commonTestsvar module를 이동하려하고, 테스트에서 그 변수를 사용하는 대신에 params.module 및 그들은 예상대로 통과합니다. 귀하의 환경에서 이것이 옵션인지는 모르겠지만 도움이되기를 바랍니다.

접견, 내 첫 번째 예는 실제로 일한 것을 지적 안드레아스

안드레아스에
+0

흥미 롭습니다. 당연히 맞습니다 : require.js입니다. 것은 'commonTests'에없는 다른 테스트가 있기 때문에 MyModule이로드되고 있다는 것을 알 수 있습니다 (간결성을 위해 생략했습니다). 그리고 그들은 잘 통과합니다. 'commonTests'에서 디버거를 실행하고'module'의 값을 검사 할 때'undefined'입니다. 'commonTests' 밖의 테스트에서 올바른 값을 가지고 있습니다 ... 감사합니다! –

+0

다른 테스트가 하단의 두 개의 'describe'호출에 있다고 가정합니다. 내가 JS에서'var' 스코프에 대해 읽은 것을 기억합니다. 경우에 따라 함수에서'var'로 정의 된 변수는'var' 문 뒤에 만 사용할 수 있습니다. 이 경우 여기에 'commonTests'에있는 전역 모듈 (존재하지 않음)에 액세스하려고합니다. 아마도'var 모듈 '과'beforeEach'를 주변의'describe'의 맨 위로 옮기려고 할 수 있습니다. – Andreas

+0

감사합니다 Andreas, 나는 모양을 가지고 내 코드와 예제를 비교했지만 약간 다릅니다. 나는 질문에 업데이 트를 게시했습니다 ... –

관련 문제