如何将工厂注入单元测试

时间:2015-01-28 23:55:39

标签: javascript angularjs unit-testing

我正在尝试将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?

})

我一直在网上搜索,找不到任何有用的信息。任何人都可以帮我吗?非常感谢!

3 个答案:

答案 0 :(得分:0)

我猜你必须把你的工厂注入你的测试。这样的事情。

beforeEach(inject(function(_$controller_, _$rootscope_, myFactory) {
    // call myFactory
})

所有文件都必须可用于测试。

答案 1 :(得分:0)

以下是测试工厂的示例。 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');
});

});

答案 2 :(得分:0)

问题是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 
    })
})
})