【发布时间】:2016-03-25 12:55:59
【问题描述】:
我无法让这两个规范文件相互配合。我不认为规范文件会影响其他规范文件,但在这种情况下,它们似乎会影响其他规范文件,这对我来说毫无意义。
我正在使用 Jasmine 和 Karma 测试是通过 Gulp 自动进行的
我得到的错误是 "Unknown provider: ProductServiceProvider
我已更改测试以解决问题,这里是简单版本。
如果我在文件 2 中注释掉以下行,则两个文件都通过。
angular.module('eu.product.service', []);
这与模拟模块有关,但我无法弄清楚我在这里做错了什么。
规格文件 1
describe('Testing euProduct', function(){
var $factory;
var $httpBackend;
beforeEach(function () {
//modules
module('eu.product.service');
//injections
inject(function($injector){
$factory = $injector.get('ProductService');
$httpBackend = $injector.get('$httpBackend');
});
//mock data
$httpBackend.when('GET', '/Mercury/product/list/0/0?PrimaryCategoryID=0&pageSize=20&startPage=1').respond({
"data":
[{
"recallid":45,
}]
});
});
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
//-----Tests----
it('Should be able to get data from the server on default parameters.', function(){
$factory.list({},function(data){
expect(data.data[0].recallid).toBe(45);
});
$httpBackend.flush();
});
});
规格文件 2
'use strict';
describe('测试euProduct逻辑', function(){
//variables in closure scope so they can be used in tested but set with injection in beforeEach
var $factory;
//mocking a module :: http://www.sitepoint.com/mocking-dependencies-angularjs-tests/
beforeEach(function () {
angular.module('eu.product.service',[]);
module(function($provide) {
$provide.factory('ProductService', function() {
// Mocking utilSvc
return {
list : function(para, callback){
callback({
data : {
product : 'The product Name'
}
})
}
};
});
$provide.service('storageSvc', function() {
// Mocking storageSvc
});
});
//modules
module('eu.product.logic');
//injections
inject(function($injector){
$factory = $injector.get('ProductLogic');
});
});
//-----Tests----
it('Should be able to run tests', function(){
expect(2).toBe(2);
});
});
【问题讨论】:
-
像这样加载模块
angular.module('eu.product.service') -
我正在拍摄模拟模块而不是导入它,因此如果 eu.product.service 模块出现错误,它将不会出现在 eu.service.logic 测试中。您将如何模拟它而不是导入它。
-
你目前的做法是,创建一个新的模块来清除它的所有注册组件。你想在
eu.product.service中模拟什么? -
我编辑了这篇文章,所以你可以看到我是如何嘲笑它的。
标签: angularjs jasmine karma-jasmine