相关疑难解决方法(0)

将模拟注入AngularJS服务

我写了一个AngularJS服务,我想对它进行单元测试.

angular.module('myServiceProvider', ['fooServiceProvider', 'barServiceProvider']).
    factory('myService', function ($http, fooService, barService) {

    this.something = function() {
        // Do something with the injected services
    };

    return this;
});
Run Code Online (Sandbox Code Playgroud)

我的app.js文件已注册:

angular
.module('myApp', ['fooServiceProvider','barServiceProvider','myServiceProvider']
)
Run Code Online (Sandbox Code Playgroud)

我可以测试DI是这样工作的:

describe("Using the DI framework", function() {
    beforeEach(module('fooServiceProvider'));
    beforeEach(module('barServiceProvider'));
    beforeEach(module('myServiceProvder'));

    var service;

    beforeEach(inject(function(fooService, barService, myService) {
        service=myService;
    }));

    it("can be instantiated", function() {
        expect(service).not.toBeNull();
    });
});
Run Code Online (Sandbox Code Playgroud)

这证明了服务可以由DI框架创建,但是接下来我想对服务进行单元测试,这意味着模拟注入的对象.

我该怎么做呢?

我已经尝试将模拟对象放在模块中,例如

beforeEach(module(mockNavigationService));
Run Code Online (Sandbox Code Playgroud)

并将服务定义重写为:

function MyService(http, fooService, barService) {
    this.somthing = function() {
        // Do something with the injected services
    };
});

angular.module('myServiceProvider', …
Run Code Online (Sandbox Code Playgroud)

javascript mocking jasmine angularjs angularjs-service

114
推荐指数
1
解决办法
10万
查看次数

AngularJS控制器单元测试 - 注入服务

一个关于AngularJS单元测试的希望简单的问题.我有一个使用简单服务的控制器(改编自angular-seed项目)

services.js:

angular.module('myApp.services', []).value('version', '0.1');
Run Code Online (Sandbox Code Playgroud)

controllers.js:

function MyCtrl1($s, version) {
  $s.version = version;
}
MyCtrl1.$inject = ["$scope","version"];
Run Code Online (Sandbox Code Playgroud)

这适用于我的应用程序.但是,我在单元测试框架工作中创建控制器时遇到问题.我无法想象我们如何注入'版本'服务(或创建实例)并将其传递给$ controller()工厂 - 我认为这就是我想要做的事情?!这是裸骨规格:

controllerSpec.js:

beforeEach(inject(function($rootScope, $controller) {
  scope = $rootScope.$new();
  // how about version service?
  ctrl = $controller(MyCtrl1, {$scope: scope, /* version: <where from?> */});
}));

it('Version should be 0.1 ...', function() {
    expect(scope.version).toBe('0.1');
});
Run Code Online (Sandbox Code Playgroud)

运行测试工具会产生: > test.sh

...失败(3.00毫秒):错误:错误:未知提供者:versionProvider < - 版本错误:未知提供者:versionProvider < - 版本

我用$ injector/$ provider和module()尝试过各种各样的东西,但无济于事.我确定答案很简单,但我看不出来.

unit-testing angularjs

37
推荐指数
1
解决办法
1万
查看次数

如何在另一个服务中从外部文件注入服务的模拟

我正在测试服务1取决于另一个服务B.通常这样做的正确方法是$provide在注入A之前使用该对象.但是,我有两个约束:

  • 服务B的模拟存储在另一个文件中,因为我在多个测试中重用它(如后面所示,我只是注册一个新的Angular服务)
  • 我需要在我的测试中的某些点初始化此服务B(设置承诺)

问题是,到目前为止(我正在测试控制器),我能够从我的测试中访问服务B,我需要注入它.但是要$provide在我的测试中模拟它,我需要在我的测试中访问它,这会产生问题,因为$provide需要在任何之前使用它inject().这是我的测试:

describe('viewsListService', function() {
    var viewList,
        queryDeferred,
        mockViews,
        $scope;

    beforeEach(module('BoardMocks'));
    beforeEach(module('Board'));

    beforeEach(inject(function(mockViewsService) {
        var query = {};
        mockViewsService.init({}, query);
        queryDeferred = query.deferred;

        mockViews = mockViewsService.mock;
    }));

    beforeEach(function() {
        angular.mock.module('Board', function ($provide) {
            $provide.value('Views', mockViews);
        });
    });

    beforeEach(inject(function(viewsListService, $rootScope) {
        $scope = $rootScope.$new();
        viewList = viewsListService;

        // Initialisation of the viewsListService
        viewList.init();
        queryDeferred.resolve([1]);
        $scope.$digest();
    }));

    describe('getAllViews', function() {
        var allViews;

        beforeEach(function() {
            allViews = viewList.getAllViews();
        });

        it('should return all the …
Run Code Online (Sandbox Code Playgroud)

jasmine angularjs

5
推荐指数
1
解决办法
1062
查看次数