簡體   English   中英

如何使用提供程序依賴項對模塊進行單元測試?

[英]How to unit test module with provider dependencies?

我有一個現有的角度應用程序。 現在,我要開始一些單元測試。 我使用茉莉花和業力來測試我的服務。 我的問題是我無法使用$ stateProvider依賴項測試服務。 我正在使用什么:-angularjs 1.5.8-angular模擬1.5.8

我的代碼:

angular.module('starter.start', []).config(function ($stateProvider) {
          // ...
}).factory('MyService', function() {
          // ...
});

考試:

describe('MyServiceSpec', function() {
    var MyService;
    beforeEach(angular.mock.module('starter.start'));
    beforeEach(inject(function(_MyService_) {
        MyService= _MyService_;
    }));

    it('should exist', function() {
        expect(MyService).toBeDefined();
    });
});

我收到錯誤Unknown provider: $stateProvider 在我的研究中,我發現需要使用$provide但是我不確定如何執行此操作。

您需要在配置文件中添加$ stateProvider

接下來,您需要在主應用程序之前注入ui.router

beforeEach(angular.mock.module('ui.router'));
beforeEach(angular.mock.module('starter.start'));

然后您需要致電服務

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM