0

我正在尝试在茉莉花中进行测试,如下所示

    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它块。

如何摆脱这种局面。

提前致谢。

4

1 回答 1

0

您的具体测试实现肯定还有另一个问题,因为一个beforeEach块只会为一个describe块中的测试运行,而不是其他的。我会假设您在一个beforeEach函数中所做的某些设置会泄漏到其他测试中,例如当您忘记 a 时var,因此该设置在所有测试中都可用。

于 2013-08-22T07:54:43.140 回答