在角度单位测试中是否有一个afterEach模块?

时间:2016-04-18 19:30:44

标签: angularjs unit-testing

我正在尝试测试一个switch语句,其中config是注入控制器的常量

vm.twoFaSecondaryMethod = config[countryCode].twoFaSecondaryMethod;

switch (vm.twoFaSecondaryMethod) {
    case 'CARDLAST4DIGIT':
        vm.twoFaSecondaryMethodInfo = translate.get('verifyOneTimePassword.cardVerificationSubHeading');
        vm.twoFaSecondaryMethodErrorText = translate.get('verifyOneTimePassword.cardVerificationError');
        break;
    case 'NATIONALID':
        vm.twoFaSecondaryMethodInfo = translate.get('verifyOneTimePassword.nationalIDSubHeading');
        vm.twoFaSecondaryMethodErrorText = translate.get('verifyOneTimePassword.nationalIDError');
        break;
    case 'POSTALCODE':
        vm.twoFaSecondaryMethodInfo = translate.get('verifyOneTimePassword.postalCodeSubHeading');
        vm.twoFaSecondaryMethodErrorText = translate.get('verifyOneTimePassword.postalCodeError');
        break;
    case 'CARDEXPIRYDATE':
        vm.twoFaSecondaryMethodInfo = translate.get('verifyOneTimePassword.cardExpirySubHeading');
        vm.twoFaSecondaryMethodErrorText = translate.get('verifyOneTimePassword.cardExpiryError');
        break;
    default:
        break;
}

我已经能够用这个来测试第一个分支'CARDLAST4DIGIT':

beforeEach(function() {
    bard.appModule('verifyOneTimePassword', function($provide) {
        $provide.value('localeService', {
            getCountry: function() {
                return 'US';
            }
        });

        $provide.constant('config', {
            us: {
                twoFaSecondaryMethod: 'CARDLAST4DIGIT',
                twoFaPrimaryMethod: 'SMS',
                phonePrefix: 'US+1'
            }
        });
    });

不幸的是,这个beforeEach with the provide是必要的,以使我的主要的beforeEach甚至可以运行,...无论如何我是用修改后的提供者重新初始化模块说

bard.appModule('verifyOneTimePassword', function($provide) {
    $provide.value('localeService', {
        getCountry: function() {
            return 'US';
        }
    });

    $provide.constant('config', {
        us: {
            twoFaSecondaryMethod: 'NATIONALID',
            twoFaPrimaryMethod: 'SMS',
            phonePrefix: 'US+1'
        }
    });
});

1 个答案:

答案 0 :(得分:0)

是的,您可以使用afterEach,如:

describe("A spec (with setup and tear-down)", function() {
    var foo;

    beforeEach(function() {
      foo = 0;
      foo += 1;
    });

    afterEach(function() {
      foo = 0;
    });

    it("is just a function, so it can contain any code", function() {
      expect(foo).toEqual(1);
    });
});

Here is the Jasmine specification