我對單元測試很新。我正在編寫代碼。我的js文件有:茉莉花嘲笑服務:缺少提供者
app.service("appService", function($http) {
this.getData = function(url) {
return $http.get(url);
}
this.foo = function() {
console.log("Hello foo function");
}
})
app.controller("productsController", ['$scope', '$http', 'appService',
function($scope, $http, appService) {
var pct = this;
console.log("This is products controller");
pct.url = "http://mysafeinfo.com/api/data?list=englishmonarchs&format=json";
var jsonDataPromise = appService.getData(pct.url);
jsonDataPromise
.then(function(response) {
pct.jsonData = response.data;
}, function(err) {
console.log("Error is: " + error);
});
}
]);
我單位在使用茉莉框架karma.js測試它。我已testfile.js爲:
describe('unitTesting', function() {
var $prodScope, prodCtrl, mockService;
beforeEach(module('sampleApp')); //Name of angular module
var jsonData = {
"name": "test_name",
"email": "test_email"
};
beforeEach(function() {
mockService = jasmine.createSpyObj('appService', ['getData', 'foo']);
})
beforeEach(inject(function($controller, $rootScope, $httpBackend, $http, $q, mockService) {
defer = $q.defer();
$prodScope = $rootScope.$new();
prodCtrl = $controller('productsController', {
$scope: $prodScope,
$http: $http,
appService: mockService
});
spyOn(mockService, "getData").and.callFake(function() {
return defer.promise;
});
spyOn(mockService, "foo");
}));
//Here unit tests go
})
我的問題是,當我使用因果報應的開始(我有karma.conf.js)運行測試,我得到錯誤:
[$ injector:unpr]未知提供者:mockServiceProvider < - mockService。
任何人都可以向我解釋爲什麼我得到這個錯誤?請解釋什麼應該是正確的代碼。
的聯繫是非常有用的。不管怎麼說,還是要謝謝你。 – Aosis