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
它阻止。
如何擺脫這種情況。
在此先感謝。
Koberle,我更新我的問題,並添加真實的代碼。我的代碼中是否有錯誤? – Ancient
'singUpViewModel' stug應該在'beforeEach'完成。還應該在每個'beforeEach'塊中重新創建間諜 –
是的,你是正確的人,'signupViewModel'創建問題。非常感謝 。 – Ancient