我在角度茉莉花测试中有以下内容......
beforeEach(module('app'));
beforeEach(module('app.services'));
beforeEach(module(function ($provide, Config) {
reservationCallerJquery = {
reserveBooking: function (reservationRequestDto, reserveBookingSuccess) {
doJqueryPost(reservationRequestDto.item,
"I need a value from my config class",
reserveBookingSuccess);
}
$provide.value("ReservationCaller", reservationCallerJquery);
}));
但我收到错误:
错误:[$ injector:modulerr]由于以下原因无法实例化模块功能($ provide,Config):错误:[$ injector:unpr]未知提供商:配置
那么如何将我的存根的字符串设置为来自配置? (Config住在' app.services')...我想我需要从那里得到它,但是如何?
答案 0 :(得分:1)
您缺少注射,您可以通过以下两种方式之一注入服务,常量,控制器等。
在注入回调中,您可以检索$ injector,然后获得您想要的内容。
var myService, location;
beforeEach(function() {
inject(function($injector) {
myService = $injector.get('myService');
location = $injector.get("$location");
});
});
或
直接获取服务,您可以添加将作为ServiceName解析的_ServiceName_,但允许您在测试中使用ServiceName var:
var scope, $rootScope, $location;
beforeEach(inject(function (_$rootScope_, _$location_) {
scope = $rootScope.$new();
$rootScope = _$rootScope_;
$location = _$location_
}));
我的典型测试设置是:
beforeEach(module('MyApp'));
beforeEach(module('MyApp.services'));
beforeEach(module('MyApp.controllers'));
var ctrl, scope, myService;
beforeEach(inject(function ($rootScope, $controller, $injector) {
scope = $rootScope.$new();
ctrl = $controller('MyCtrl', {$scope: scope});
myService = $injector.get('MyService');
}));