我在我的一个运行块中测试代码。这是迄今为止的测试代码:
(function() {
'use strict';
describe('preflight.config',function() {
var $rootScope, $state, jwtHelper, store;
var id_token = "fdsafdsfsadfsadssafdsafs.eyJjb250YWN0Ijp7ImNyZWF0ZWRfYXQiOiIyMDE1LTExLTIzIDE3OjQyOjE1IFVUQyIsImdpZCI6ImdpZDovL2Zsb3dsZW5zL0NvbnRhY3QvMSIsImlkIjoxLCJ0b19zIjoiRGlhbm5hIEJhbGR3aW4iLCJ1cGRhdGVkX2F0IjoiMjAxNS0xMi0wNyAxNDozMzoxMSBVVEMiLCJjYW5fc2lnbl9pbiI6dHJ1ZSwiZW1haWwiOiJhbmRyZXcuY29jaHJhbmVAZmxvd2xlbnMuY29tIiwibmFtZSI6IkRpYW5uYSBCYWxkd2luIn0sImV4cCI6MTQ1MDE5NDg5MX0.EbXyENqN6YqYVrwy69iQq8AjoeEpjFcO23DdQChk7hI";
var tokenPayload = {
contact: {
name: 'Flerp Derp',
age: 26
}
};
beforeEach(function() {
module('app', function($provide) {
var storeSpy = jasmine.createSpy().and.returnValue(id_token);
var jwtSpy = jasmine.createSpy().and.returnValue(tokenPayload);
$provide.value('jwtHelper', {
decodeToken: jwtSpy
});
$provide.value('store', {
get: storeSpy
});
});
inject(function(_$rootScope_, _$state_, _jwtHelper_, _store_) {
$rootScope = _$rootScope_;
$state = _$state_;
jwtHelper = _jwtHelper_;
store = _store_;
});
spyOn($rootScope,'$on');
});
afterEach(function() {
// How should this be properly torn down?
});
describe('store',function() {
it('get() should be called with "id_token"',function() {
expect(store.get).toHaveBeenCalledWith('id_token');
});
});
describe('$rootScope',function() {
it('should be set tokenPayload.contact if jwt and contact is present',function() {
expect($rootScope.currentUser).toBe(tokenPayload.contact);
});
});
});
}());
这很好 - 这些测试通过了。但是我必须使用fdescribe
语法单独运行此测试,因为它打破了我的其他一些测试。
我认为这是因为我使用$provide
来构建某些依赖项的间谍只是因为它们出现在运行块中。
有没有其他人遇到过这种情况?我确定我需要正确拆除afterEach
区块中的测试,但我不确定如何。
有什么建议吗?