beforeEach正在编写所有描述

时间:2013-08-22 07:43:59

标签: unit-testing bdd jasmine

我正试图在茉莉花中进行测试,如下面的

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

如何摆脱这种情况。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您的具体测试实现必定存在另一个问题,因为beforeEach块仅在describe块中运行而不在其他块中运行。我假设您在一个beforeEach函数中进行的某些设置会泄漏到其他测试中,就像您忘记了var一样,因此该设置在所有测试中都可用。

相关问题