如何将工厂注入单元测试
How to inject factory into unit test
我正在尝试将我的factory
注入我的单元测试中。
我的文件中有一些东西,例如
//inside my 'testCtrl' I have
$scope.getProduct = function() {
//other codes..
myFactory.getProduct()
.then(function(obj){
$scope.product.id = obj.id;
})
}
我的工厂文件
angular.module('myApp').factory('myFactory', function($http, $q) {
//codes...
})
我的测试文件。
describe('unit test here', function(){
beforeEach(module('myApp'));
beforeEach(module('myFactory'));
beofreEach(inject(function(_$controller_, _$rootscope_) {
scope._$rootScope.$new();
testCtrl = _$controller_('testCtrl', {
$scope:scope
})
})
//I got a Module 'plannerFactory' is not available error
//How do I inject myFactory into my test file here and how do I //use it?
})
我一直在网上搜索,找不到任何有用的信息。谁能帮我?多谢!
我想
你必须将你的工厂注入到你的测试中。像这样的东西。
beforeEach(inject(function(_$controller_, _$rootscope_, myFactory) {
// call myFactory
})
所有文件都必须可用于测试。
下面是测试工厂的示例。 RestService是我正在测试的工厂:
describe('RestService', function() {
var restService;
var $httpBackend;
var url = 'http://ram-utilities.com/{0}/test/{1}';
var argList = ['jasmine', 2015];
// Initialization of the AngularJS application before each test case
beforeEach(module('ram-utilities.ui.rest.service'));
// Injection of dependencies
beforeEach(inject(function(_RestService_, _$httpBackend_) {
restService = _RestService_;
$httpBackend = _$httpBackend_;
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('should send an HTTP GET request', function(){
$httpBackend.expectGET('http://ram-utilities.com/jasmine/test/2015')
.respond(200, {message: 'GET Successful', id: 0});
restService.getData(url, argList, null, {})
.then(function(data){
expect(data).toBeTruthy();
expect(data.message).toBe('GET Successful');
});
$httpBackend.flush();
});
it('should send an HTTP POST request', function(){
$httpBackend.expectPOST('http://ram-utilities.com/jasmine/test/2015', {data: 'Test POST'})
.respond(200, {message: 'POST Successful', id: 0});
restService.postData(url, argList, null, {data: 'Test POST'}, {})
.then(function(response){
expect(response).toBeTruthy();
expect(response.success).toBe(true);
});
$httpBackend.flush();
});
it('should execute the function passed into configureSessionTimeOut', function(){
$httpBackend.expectGET('http://ram-utilities.com/jasmine/test/2015')
.respond(401, {message: 'timed out', id: 0});
// setup the conditions to be tested
var testResult = '';
var testFn = function(){
testResult = 'session did time out';
};
restService.configureSessionTimeOut(testFn);
restService.getData(url, argList, null, null, null, null, null);
$httpBackend.flush();
expect(testResult).toBe('session did time out');
});
});
问题是myFactory不是一个模块,所以你不能按照你尝试的方式注入它。
describe('unit test here', function(){
var myFactory;
beforeEach(module('myApp'));
beforeEach(inject(function(_$controller_, _$rootscope_, _myFactory_) {
//Now you can use myFactory in your specs..
myFactory = _myFactory;_
scope._$rootScope.$new();
testCtrl = _$controller_('testCtrl', {
$scope:scope
})
})
})
相关文章:
- 我的单元测试选项是什么
- 如何在单元测试中将存储注入Ember.Service
- 如何在单元测试中注入Angular控制器
- 如何将工厂注入单元测试
- 为什么在单元测试中注入“ng”会改变承诺处理行为
- 无法将服务注入其 Angular 单元测试中
- 在 Karma/Jasmine 单元测试中注入 Angular $timeout服务的真实实现
- 将模拟提供程序注入到角度单元测试中
- 单元测试服务不;t注入工厂
- Angularjs,Karma控制器单元测试注入器:modulerr
- 用注入服务对angular控制器进行单元测试
- Angular单元测试,模拟使用$injector注入的服务
- 使用karma angularjs在单元测试中注入问题
- 角1.单元测试中的X服务注入
- 如何在AngularJS单元测试中注入$rootScope ?
- 在Angular 2 2.0.0-rc.5的单元测试中注入依赖项
- 在Jasmine中对Angular进行单元测试时,将模拟自定义提供程序注入到提供程序中
- 单元测试-使用应用、调用或绑定方法在JavaScript中注入依赖项
- 如何将ngRoute注入Jasmine / Karma AngularJS单元测试
- 在单元测试AngularJS服务时注入依赖服务