我正在尝试在茉莉花中进行测试,如下所示
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('ss@ss.com');
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();
});
});
});
});
});
});
现在我想要在beforeEach
中定义的SUB1
,应该只适用于所有it
可用的,SUB1
但它也适用于SUB2
它块。
如何摆脱这种局面。
提前致谢。