簡體   English   中英

如何將工廠注入單元測試

[英]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 
    })
})
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM