小编Mic*_*vin的帖子

使用业力注入$ stateParams进行角度单元测试测试

让我们从控制器代码开始:

angular
.module('hc.hotelContent')
.controller('NameLocationController', nameLocationCtrlFn); //Todo change hotelDataService
nameLocationCtrlFn.$inject = ['$stateParams', 'hotelDataService'];

  function nameLocationCtrlFn($stateParams, hotelDataService) {
       var vm = this,
       hotelId = $stateParams.hotelId;
       vm.Keys = {
        Name: 'Name',
        Street: 'Street',
        City: 'City',
        State: 'State',
        Zip: 'Zip',
        Country: 'Country'
    }
}
Run Code Online (Sandbox Code Playgroud)

我确实有更多的代码,但它的相关逻辑,我没有将$ stateParmas注入控制器时我的测试工作正常.
所以继承测试文件:

    describe('nameLocation component Controller', function () {
    var $controller,
            hotelDataServiceMock,
           stateParams,
            hotelId = "4611";
Keys = {
        Name: 'Name',
        Street: 'Street',
        City: 'City',
        State: 'State',
        Zip: 'Zip',
        Country: 'Country'
    }//@
    beforeEach(module('hc.hotelContent'));
        beforeEach(module('hc.app.core'));
        beforeEach(inject(injectFn));
       function injectFn(_$controller_, $stateParams, _hotelDataService_) { …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing jasmine angularjs karma-runner

4
推荐指数
1
解决办法
2167
查看次数