错误:[$ injector:unpr]在控制器中注入routeprovider服务时,jasmine测试中的未知提供程序

时间:2014-09-29 14:25:56

标签: javascript angularjs junit jasmine

我已经在角度js中创建了一个应用程序,我使用ng-view来导航模板,来自routeProvider我正在向所有模板注入一个名为customerDetail的服务。当我编写一个用于将customerDetail服务注入CustomerReportController构造函数的jasmine测试用例时,但我得到了

<failure type="">Error: [$injector:unpr] Unknown provider: customerDetailProvider &lt;- customerDetail

任何人都可以告诉我一些解决方案吗

主要/ customerReport / main.spec.js

describe('tsi', function() 
{
    var $scope, customerDetail;

    beforeEach(module('tsi.customerReport'));

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

        CustomerReportController = $controller('CustomerReportController', {
            $scope: $scope,
            customerDetail: customerDetail
        });

    }));

    it('test CustomerReportController', inject(function() {
        expect(CustomerReportController).toBeTruthy();
    }));
});

main.js

angular.module( 'tsi', ['tsi.customerDetail', 'ngRoute'])

.config(function(RestangularProvider, $routeProvider)
{
    RestangularProvider.setBaseUrl('/customer/service/detail');

    $routeProvider.when('/customerDetail',
        {
            templateUrl: 'main/main.tpl.html',
            controller: 'CustomerDetailController',
            resolve: {
                customerDetail: function(CustomerDetailService) {
                   return CustomerDetailService.getServiceDetail();
                }
            }
        }).when('/customerReport',
        {
            templateUrl: 'main/customerReport/main.tpl.html',
            controller: 'CustomerReportController',
            resolve: {
                customerDetail: function(CustomerDetailService) {
                   return CustomerDetailService.getServiceDetail();
                }
            }
        }).otherwise(
        {
            redirectTo: '/customerDetail'
        });
})

.factory('CustomerDetailService', function(Restangular) {
    return {
        getCustomerDetail: function() {
            return Restangular.one('user/customerDetail').get().then(function(customerDetail) {
                return customerDetail;
            });
        }
    };
});

主要/ customerReport / main.js

angular.module('tsi.customerReport', [])

.controller('CustomerReportController', function($scope, $http, $filter, $timeout, customerDetail) 
{
    $scope.customerOrderDetails = customerDetail;
    :
    :
});

1 个答案:

答案 0 :(得分:0)

我面临同样的问题.. 我做的是我在我的js文件中包含了所有服务。确保所有服务和提供者都正确注入

这是茉莉花单元测试的示例代码。

 describe('UserService',function() {
   var scope, DataService, UserService, http, cookieStore;
    beforeEach(module('app'));

    beforeEach(inject(function ($http, _DataService_) {

        http = $http;

        DataService = _DataService_;

    }));
    it('getProfilePicture', function () {
        http.post('/api/getProfilePicture', function (data) {

            expect(data).toBeDefined();

        });


    });

});

DataService是我的服务