2013-08-22 48 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