2014-11-10 5 views
0

javascript 사양에 대해 Jasmine 2.X와 함께 AngularJS 1.2.16을 사용합니다.
그러나 그들은 빨리 지저분 해졌습니다. 리팩터링 및 스펙 구성 방법에 관한 정보를 찾는 데 어려움을 겪고 있습니다. 여기 AngularJS - 재스민 스펙을 어떻게 리펙토링 할 수 있습니까?

이 나쁜 광산의 사양은 다음과 같습니다 그들은 내가 더 넣어 시작하는 그들에있는 모든 개체를 너무 오래 때문에

channel = mockRestangular = $httpBackend = deferred = undefined 
    channel_id = {...} 

    beforeEach -> 
    module("channels", ($provide) -> 
     mockRestangular = { 
     configuration: { baseUrl: "" } 
     one: -> 
      this 
     post: -> 
      this 
     put: -> 
      this 
     ... 
     } 

     module ($provide) -> 
     $provide.value('Restangular', mockRestangular) 
     return 
    ) 

    beforeEach inject((_channel_, $q, $injector) -> 
    channel = _channel_ 
    $httpBackend = $injector.get('$httpBackend') 
    deferred = $q.defer() 
) 

    it "spec1", inject(($q, $rootScope) -> 
     deferred = $q.defer() 
     spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise) 
     spyOn(channel::, 'init').and.stub() 
     new_channel = new channel(channel_id) 
     new_channel.updateCount() 
     deferred.resolve({"channels":[{...long...long...object...}]}) 
     $rootScope.$digest() 
     expect(new_channel.meta.totalProducts).toEqual(24849) 
     expect(new_channel.meta.activeProducts).toEqual(1349) 
    ) 

    it "spec2", inject(($q, $rootScope) -> 
     deferred = $q.defer() 
     spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise) 
     spyOn(channel::, 'init').and.stub() 
     new_channel = new channel(channel_id) 
     new_channel.updateStatisticsRevenue() 
     deferred.resolve({"revenue_statistics":[{...another...very...very...long...object...}]}) 
     $rootScope.$digest() 
     expect(new_channel.statistics.revenue).toEqual([{...kinda...long...object...result...}]) 
    ) 

    # spec with real respond-mock objects 
    describe "describtor2", -> 
    it "spec3", inject(($rootScope) -> 
    $httpBackend.expectPUT().respond(201, 
    {products:[{"sku":"10413161","active":false,"min_price":{"fractional":400,"currency":"EUR"},"max_price":{"fractional":950,"currency":"EUR"}},{"sku":"10413162","active":true,"min_price":{"fractional":458,"currency":"EUR"},"max_price":{"fractional":799,"currency":"EUR"}}]}) 
    spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise) 
    spyOn(channel::, 'init').and.stub() 
    new_channel = new channel channel_id 
    new_channel.updateProducts() 
    new_channel.getMeta().activeProducts = 2 
    expect(mockRestangular.one().one().get).toHaveBeenCalled 
    deferred.resolve({"products":[{"sku":"10413161","active":true,"min_price":{"fractional":412,"currency":"EUR"},"max_price":{"fractional":890,"currency":"EUR"}},{"sku":"10413162","active":true,"min_price":{"fractional":448,"currency":"EUR"},"max_price":{"fractional":799,"currency":"EUR"}}]} 
    ) 
    $rootScope.$digest() 
    new_channel.updateProduct([{sku:"10413161",active:false,min_price:{fractional:400,currency:"EUR"},max_price:{fractional:950,currency:"EUR"}},{"sku":"10413162","active":true,"min_price":{"fractional":458,"currency":"EUR"},"max_price":{"fractional":799,"currency":"EUR"}}]) 
    $httpBackend.flush() 
    expect(new_channel.getProducts()).toEqual(
[{"sku":"10413161","active":false,"min_price":{"fractional":400,"currency":"EUR"},"max_price":{"fractional":950,"currency":"EUR"}},{"sku":"10413162","active":true,"min_price":{"fractional":458,"currency":"EUR"},"max_price":{"fractional":799,"currency":"EUR"}}] 
    ) 
    expect(new_channel.getMeta().activeProducts).toBe(1) 
) 

하나의 스펙으로 "기대". 나는 그것이 틀렸다는 것을 알고 있지만, 나는 그 거대한 스펙을 두려워하고있다.

Jasmine 사양을 구조화하거나 리팩토링하는 모범 사례가 있습니까?

답변

1

사용 BeforeEach는 예를 들어, 당신은 그 라인을 넣을 수, 각 사양의 몇 가지 초기 공통 코드를 넣어 :

 deferred = $q.defer() 
     spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise) 
     spyOn(channel::, 'init').and.stub() 
     new_channel = new channel(channel_id) 

BeforeEach에서 관심을 describe에 관련.

beforeEach(function() { 
     deferred = $q.defer(); 
     spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise); 
     spyOn(channel::, 'init').and.stub(); 
     new_channel = new channel(channel_id); 
}); 

다른 대안 : 공통 코드를 수집하기위한 몇 가지 기본 javascript 기능을 만듭니다. 장점이 될 것이다 당신 수 이름 코드의 그 부분 :

beforeEach` 좋은`그건
function mockDBGet() { 
      deferred = $q.defer(); 
      spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise); 
} 

function initChannel() { 
      spyOn(channel::, 'init').and.stub(); 
      new_channel = new channel(channel_id); 
} 
//....... 
it('myCurrentSpec', function(){ 
    mockDBGet(); 
    initChannel(); far more clean than your previous version 
}); 
+0

. 몰랐던 모든 사람들을 위해 - 나는 그랬듯이 - 모든 기술은 자신의 'beforeEach' 기능을 가질 수 있습니다. 흥미로운 다른 대안 사운드, 몇 가지 예/추가 링크를 제공 할 수 있습니까? –

+0

예를 들어 업데이트되었습니다. – Mik378

+0

Ohh ja. 아름답습니다. 좀 더 큰 모의 응답 객체를 처리하는 방법에 대해 생각해 보셨습니까? 우리가 이것을 추가 할 수 있다면, 당신은 A + 답을 주었고, 저와 다른 사람들을위한 모든 질문에 관한 것입니다. –