[英]How to inject factory into unit test
I am trying to inject my factory
into my unit test. 我正在尝试将factory
注入单元测试中。
I have something in my file like 我的档案中有东西
//inside my 'testCtrl' I have
$scope.getProduct = function() {
//other codes..
myFactory.getProduct()
.then(function(obj){
$scope.product.id = obj.id;
})
}
my factory file 我的工厂文件
angular.module('myApp').factory('myFactory', function($http, $q) {
//codes...
})
my test file. 我的测试文件。
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?
})
I have been searching on the web and couldn't find any useful info. 我一直在网上搜索,找不到任何有用的信息。 Could anyone help me about it? 有人可以帮我吗? Thanks a lot! 非常感谢!
I guess you have to inject your factory into you test. 我想您必须将工厂注入测试中。 Something like this. 这样的事情。
beforeEach(inject(function(_$controller_, _$rootscope_, myFactory) {
// call myFactory
})
All the files must be available for the test. 所有文件必须可用于测试。
Here is an example of testing a factory. 这是测试工厂的示例。 RestService is my factory that is being tested: 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');
});
}); });
The problem is that myFactory is not a module, so you cannot inject it the way you are trying to. 问题在于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
})
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.