如何将工厂注入单元测试

How to inject factory into unit test

本文关键字:注入 单元测试 工厂      更新时间:2023-09-26

我正在尝试将我的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 
    })
})
})