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