问题
Why when I run this in the karma runner:
describe('Service tests', function () {
var DataServiceMock
var httpBackend;
beforeEach(angular.mock.module('app'));
beforeEach(angular.mock.inject(function( $httpBackend, $service, DataService, $injector){
results in this error
Error: [$injector:unpr] Unknown provider: $serviceProvider <- $service http://errors.angularjs.org/1.2.1/$injector/unpr?p0=%24serviceProvider%20%3C-%20%24service at /home/site/angular/angular.js:78:12
Edit 2____________________
I'm trying to mock the DataService for the dataHandlerService
describe('Service', function () {
var DataServiceMock
var httpBackend;
var testUrl = "test/";
beforeEach(angular.mock.module('app'));
beforeEach(angular.mock.inject(function(){
module(function ($provide) {
$provide.value('DataService', DataServiceMock)
})
}));
it('should have', inject(function(DataService) {
expect('DataService').not.toBe(null);
}));
and this error:
Error: Injector already created, can not register a module! at workFn (/home/me/root/angular/angular-mocks.js:1985:15)
回答1:
There is no $service. If you want to mock your service then you should do it by using $provide:
beforeEach(function () {
DataServiceMock= {}
DataServiceMock.doSomething = function() {}
module(function ($provide) {
$provide.value('DataService', DataServiceMock)
})
})
来源:https://stackoverflow.com/questions/21186368/angular-injecting-service-results-in-unknown-provider-serviceprovider