2016-09-14 42 views
0

我一直在尝试使用茉莉花以下教程测试的角度服务测试的角度服务(不出厂),但由于某种原因,他们的例子并没有这样做的工作(他们建议使用angular.mock.inject()方法注入的服务) ...单位使用茉莉花

这是我得到它的工作方式,但恐怕这是不是应该怎么做......

这是“好的做法”?为什么注射不起作用?

我基本上导入服务进入测试,设置我的模块和$provide服务的依赖,并new服务传递什么通常会被注入...

不管怎么说,那就是:

import rolesService from './roles.service.js'; 

describe('Roles',() => { 
    let RolesService; 
    let PermRoleStore; 
    let USER; 

    beforeEach(() => { 
    angular.mock.module('roles', ($provide) => { 
     $provide.constant('USER', { 
     roles: ['SOUTIEN_ORGANISME'] 
     }); 
     $provide.value('PermRoleStore', { 
     defineManyRoles: jasmine.createSpy(), 
     }); 
    }); 

    angular.mock.inject((_PermRoleStore_, _USER_) => { 
     PermRoleStore = _PermRoleStore_; 
     USER = _USER_; 

     RolesService = new rolesService(PermRoleStore, USER); 
    }); 
    }); 

    it('Setup should define the roles',() => { 
    RolesService.setup(); 
    expect(PermRoleStore.defineManyRoles).toHaveBeenCalled(); 
    }); 

    describe('authorize',() => { 
    it('should return true if authorized',() => { 
     expect(RolesService.authorize('SOUTIEN_ORGANISME')).toBe(true); 
    }); 

    it('should return false if the user it NOT authorized',() => { 
     expect(RolesService.authorize('NOT_AUTHORIZED')).toBe(false); 
    }); 
    }); 
}); 

这里是karma.config.js文件仅供参考:

'use strict'; 

const stringify = require('stringify'); 
const babelify = require('babelify'); 

module.exports = (config) => { 
    config.set({ 
    basePath: '', 
    frameworks: ['browserify', 'jasmine-ajax', 'jasmine'], 

    files: [ 
     { pattern: 'build/gouvernementales/app-gouvernementales.config.json', watched: true, served: true, included: false }, 
     'build/gouvernementales/js/gouvernementales-libs.js', 
     'src/apps/gouvernementales/app-gouvernementales.js', 
     'src/apps/gouvernementales/**/*.spec.js', 
     'src/modules/**/*.spec.js', 
    ], 

    preprocessors: { 
     'src/apps/gouvernementales/app-gouvernementales.js': 'browserify', 
     'src/apps/gouvernementales/**/*.spec.js': 'browserify', 
     'src/modules/**/*.spec.js': 'browserify', 
    }, 

    browsers: ['PhantomJS'], 

    plugins: [ 
     'karma-phantomjs-launcher', 
     // 'karma-chrome-launcher', 
     'karma-jasmine-ajax', 
     'karma-jasmine', 
     'karma-browserify', 
     'karma-coverage', 
     'karma-mocha-reporter', 
    ], 

    browserify: { 
     debug: true, 
     transform: [ 
     babelify, 
     stringify, 
     ], 
    }, 

    helpers: [ 
     'src/spec/helpers/**/*.js', 
    ], 

    reporters: [ 
     'mocha', 
     'coverage', 
    ], 

    coverageReporter: { 
     dir: 'coverage/', 
     reporters: [ 
     { type: 'text-summary' }, 
     { type: 'html' }, 
     ], 
    }, 

    logLevel: config.LOG_DEBUG, 

    singleRun: false, 

    colors: true, 

    autoWatch: true, 
    }); 
}; 
+0

问题是什么呢? –

+0

每个教程都提到你应该在'angular.mock.inject()'方法中注入你的服务,但是在我的情况下它不起作用......不知道为什么。 – justinledouxweb

+0

我也想知道如果这样做它在我的例子做的方式是“良好做法”或不... – justinledouxweb

回答

0

你不需要做升IKE这一

import rolesService from './roles.service.js'; // it should be not included. 
//Should be injected 

BEFOREEACH

beforeEach(() => { 
    angular.mock.module(($provide) => { 

     $provide.constant('USER', { 
     roles: ['SOUTIEN_ORGANISME'] 
     }); 
     $provide.value('PermRoleStore', { 
     defineManyRoles: jasmine.createSpy(), 
     }); 

    }); 

    //you are mocking PermRoleStore and USER,so should be inject it here. 
//It will get available to your service 
//no need of import 

    angular.mock.inject((_rolesService_) => { 
     PermRoleStore = _rolesService_; 
    }); 
    }); 
+0

正如我在描述中提到的,这样做不工作... – justinledouxweb

+0

请检查'因果报应files'财产.config.js并确保列表中包含相应的文件。 –

+0

我已将karma.config.js文件添加到描述中。正如你所看到的,app-gouvernementales.js文件已经在文件列表中,并且是应用程序的入口点。理论上,根据我过去测试过的所有其他应用程序,我应该能够使用'angular.mock.inject'将我的服务注入到测试中,但出于某种奇怪的原因,它只是赢得了' t work ... – justinledouxweb