2013-08-22 49 views
0

我試圖使茉莉花測試喜歡以下beforeEach工作在所有描述

describe('Signup', function() { 

    describe('Create a new account manually', function() { 

     describe('Given : SignUp view model with valid inputs', function() { 
      var postSpy; 
      var deferred; 
      var redirectSpy; 

      beforeEach(function() { 
       deferred = deferred || $.Deferred(); 
       postSpy = postSpy || spyOn($, 'ajax').andReturn(deferred.promise()); 
       redirectSpy = spyOn(window, 'redirect'); 
      }); 
      signUpViewModel = new SignUpViewModel(); 
      signUpViewModel.name('ss'); 
      signUpViewModel.email('[email protected]'); 
      signUpViewModel.password('mypwds'); 
      signUpViewModel.confirmPassword('mypwds'); 
      signUpViewModel.company('ss'); 
      signUpViewModel.selectedCountry('UK'); 

      it('When : signup is called', function() { 
       signUpViewModel.signup(); 
      }); 
      it("then : it should post input data to /account/signup", function() { 
       var data = { 
        name: signUpViewModel.name(), 
        email: signUpViewModel.email(), 
        password: signUpViewModel.password(), 
        ConfirmPassword: signUpViewModel.confirmPassword(), 
        CompanyName: signUpViewModel.company(), 
        country: signUpViewModel.selectedCountry() 
       }; 
       expect(postSpy).toHaveBeenCalled(); 
      }); 
      it("and : it should redirect to pendingapproval page", function() { 

       deferred.resolve({ redirect: "/account/pendingapproval", success: true }); 

       expect(redirectSpy).toHaveBeenCalled(); 

      }); 

     }); 

    }); 

    describe("Validate mandatory fields", function() { 


     describe("Given : SignUp view model with invalid inputs", function() { 
      var postSpy; 
      var deferred; 
      var redirectSpy; 

      beforeEach(function() { 
       deferred = deferred || $.Deferred(); 
       postSpy = postSpy || spyOn($, 'ajax').andReturn(deferred.promise()); 
       redirectSpy = spyOn(window, 'redirect'); 
      }); 
      signUpViewModel = new SignUpViewModel(); 
      signUpViewModel.name(""); 
      signUpViewModel.email(""); 
      signUpViewModel.password(""); 
      signUpViewModel.confirmPassword(""); 
      signUpViewModel.company(""); 
      signUpViewModel.selectedCountry(""); 

      it("when : signup is called", function() { 
       signUpViewModel.signup(); 
      }); 
      it("then : it should validate view model", function() { 
       expect(signUpViewModel.errors().length).toBeGreaterThan(0); 
      }); 
      it("and : it should not make request to /account/register", function() { 
       expect(postSpy).wasNotCalled(); 
      }); 

     }); 
    }); 

    }); 
    }); 
}); 

現在我想,這是在SUB1定義beforeEach,應僅適用於SUB1所有可用it但它也工作工作爲SUB2它阻止。

如何擺脫這種情況。

在此先感謝。

回答

0

您的具體測試實施必須存在另一個問題,因爲beforeEach塊只會在describe塊中運行而不是其他測試。我會假設你在一個beforeEach函數中做出的某些設置會泄漏到其他測試中,比如當你忘記了var,因此該設置在所有測試中都可用。

+0

Koberle,我更新我的問題,並添加真實的代碼。我的代碼中是否有錯誤? – Ancient

+0

'singUpViewModel' stug應該在'beforeEach'完成。還應該在每個'beforeEach'塊中重新創建間諜 –

+0

是的,你是正確的人,'signupViewModel'創建問題。非常感謝 。 – Ancient