gpt4 book ai didi

javascript - 错误: [$injector:unpr] Unknown provider in jasmine test when routeprovider service is injected in a controller

转载 作者:行者123 更新时间:2023-11-28 07:55:48 25 4
gpt4 key购买 nike

我在 Angular JS 中创建了一个应用程序,其中我使用 ng-view 来导航模板,从routeProvider我将一个名为 customerDetail 的服务注入(inject)到所有模板中。当我编写一个 jasmine 测试用例以将 customerDetail 服务注入(inject) CustomerReportController 构造函数时,但我得到了

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

谁能告诉我一些解决方案

ma​​in/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();
}));
});

ma​​in.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;
});
}
};
});

ma​​in/customerReport/main.js

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

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

最佳答案

我也遇到同样的问题..我所做的是将所有服务包含在我的 js 文件中。确保所有服务和提供者都正确注入(inject)

这里是 jasmine 单元测试的示例代码。

 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 是我这里的服务

关于javascript - 错误: [$injector:unpr] Unknown provider in jasmine test when routeprovider service is injected in a controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26102244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com