2016-01-27 26 views
0

我是新來角和業力測試案例。如何在業力茉莉花測試用例中注入外部提供者?

對於我的應用程序,我使用Angularjs和SweetAlert來顯示警報。 (SweetAlert Plugin here

下面是我得到的HTTP請求的狀態代碼,我在SweetAlert顯示HTTP請求狀態

(function() { 
'use strict'; 

angular 
    .module('app.admin') 
    .controller('TestController', TestController); 

TestController.$inject = ['$scope','$http','SweetAlert']; 
function TestController($scope,$http,SweetAlert) { 
var vm = this; 

vm.test = 'hi'; 
    vm.todos = []; 
    vm.todo = 'Run'; 
    vm.status = ''; 

vm.GetURL = function(){ 
    $http.get("some url") 
        .then(function (result) { 
         vm.status = result.status; 
         SweetAlert.swal(JSON.stringify(vm.status)) 
        }); 
}; 

vm.addTodo = function(){ 
    vm.todos.push(vm.todo); 
}; 
vm.removeTodo = function(index){ 
    vm.todos.splice(index, 1); 
}; 

} 

})(); 

這是沒有任何問題工作正常,但我需要測試我的應用程序,因爲我正在用茉莉花架構的業力。以下是我的karma.config.js和測試文件。

karma.config.js

// Karma configuration 
// Generated on Tue Jan 26 2016 21:38:16 GMT+0530 (India Standard Time) 

module.exports = function(config) { 
    config.set({ 

    // base path that will be used to resolve all patterns (eg. files, exclude) 
    basePath: '', 


    // frameworks to use 
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
    frameworks: ['jasmine'], 


    // list of files/patterns to load in the browser 
    files: [ 
    '../app/js/base.js', 
    '../global_URL.js', 
    'bower_components/sweetalert/dist/sweetalert.css', 
    'bower_components/sweetalert/dist/sweetalert.min.js', 
    'bower_components/angular-mocks/angular-mocks.js', 
    '../app/js/app.js', 
    'test/spec/**/*.js' 
    ], 


    // list of files to exclude 
    exclude: [ 
    ], 


    // preprocess matching files before serving them to the browser 
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
    preprocessors: { 
    }, 


    // test results reporter to use 
    // possible values: 'dots', 'progress' 
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter 
    reporters: ['progress'], 


    // web server port 
    port: 9876, 


    // enable/disable colors in the output (reporters and logs) 
    colors: true, 


    // level of logging 
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
    logLevel: config.LOG_INFO, 


    // enable/disable watching file and executing tests whenever any file changes 
    autoWatch: false, 


    // start these browsers 
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
    browsers: ['Chrome'], 
    plugins: [ 
       'karma-chrome-launcher', 
       'karma-firefox-launcher', 
       'karma-jasmine', 
       'karma-phantomjs-launcher' 
      ], 


    // Continuous Integration mode 
    // if true, Karma captures browsers, runs the tests and exits 
    singleRun: false, 

    // Concurrency level 
    // how many browser should be started simultaneous 
    concurrency: Infinity 
    }) 
} 

test.spec.js

'use strict'; 

describe('Test Controller', function() { 

    // load the controller's module 
    var MainCtrl, 
     SweetAlert, 
     URLService, 
     httpcall, 
     scope; 
    beforeEach(module('sample')); 

    /*beforeEach(inject(function(_SweetAlert_,_URLService_){ 
    URLService = _URLService_; 
    SweetAlert = _SweetAlert_; 
    }));*/ 

    // Initialize the controller and a mock scope 
    beforeEach(inject(function ($rootScope, $controller,$http,_SweetAlert_) { 
    scope = $rootScope.$new(); 
    MainCtrl = $controller('TestController', { 
     $scope: scope, 
     httpcall : $http, 
     SweetAlert : _SweetAlert_ 
    }); 
    })); 

    it('should tell hi', function() { 
     expect(MainCtrl.test).toBe("hi"); 
    }); 

    it('should give the status', function() { 
     MainCtrl.GetURL(); 
     expect(MainCtrl.status).toBe(200); 
    }); 

    it('should have no items to start', function() { 
    expect(MainCtrl.todos.length).toBe(0); 
    }); 

    it('should add items to the list', function() { 
    MainCtrl.todo = 'Test 1'; 
    MainCtrl.addTodo(); 
    expect(MainCtrl.todos.length).toBe(1); 
    }); 

    it('should add then remove an item from the list', function() { 
    MainCtrl.todo = 'Test 1'; 
    MainCtrl.addTodo(); 
    MainCtrl.removeTodo(0); 
    expect(MainCtrl.todos.length).toBe(0); 
    }); 

}); 

但它引發以下錯誤:

F:\Projects\Angular\NewSample\Development\test\master>gulp test 
    [13:06:40] Using gulpfile F:\Projects\Angular\NewSample\Development\test\master\gulpfile.js 
    [13:06:40] Starting 'test'... 
    WARN `start` method is deprecated since 0.13. It will be removed in 0.14. Please 
    use 
     server = new Server(config, [done]) 
     server.start() 
    instead. 
    27 01 2016 13:06:40.250:INFO [karma]: Karma v0.13.19 server started at http://lo 
    calhost:9876/ 
    27 01 2016 13:06:40.261:INFO [launcher]: Starting browser Chrome 
    27 01 2016 13:06:41.793:INFO [Chrome 47.0.2526 (Windows 8.1 0.0.0)]: Connected o 
    n socket /#hmyJFO5x2TLTkMMHAAAA with id 83740230 
    Chrome 47.0.2526 (Windows 8.1 0.0.0) LOG: 'localhost' 

    Chrome 47.0.2526 (Windows 8.1 0.0.0) LOG: 'localhost' 

    Chrome 47.0.2526 (Windows 8.1 0.0.0) Test Controller should tell hi FAILED 
      Error: [$injector:unpr] Unknown provider: SweetAlertProvider <- SweetAle 
    rt 
      http://errors.angularjs.org/1.4.2/$injector/unpr?p0=SweetAlertProvider%2 
    0%3C-%20SweetAlert 
       at F:/Projects/Angular/NewSample/Development/test/ap 
    p/js/base.js:9274:12 

我不不知道爲什麼。,我該如何解決這個問題?

爲此我花了很長時間,是否有可能在業力中包含第三方插件。

請幫助我,在此先感謝。

回答

1

在注射之前,你應該添加這一行。

beforeEach(module('_SweetAlert_')); 

並且還像下面一樣更改karma.config.js文件路徑。

bower_components/sweetalert/dist/sweetalert.min.jsapp/bower_components/sweetalert/dist/sweetalert.min.js

+0

我加入這個beforeEach(模塊( '_ SweetAlert _')); MYAPP模塊後,它的投錯誤如_SweetAlert_不可用 – selvam

+0

我認爲你的配置文件url是錯誤的。將此'bower_components/sweetalert/dist/sweetalert.min.js'改爲'app/bower_components/sweetalert/dist/sweetalert.min.js' – JiLaBaJi

+0

我的路徑是正確的 @ balamurugan -r我的項目結構是project_name - >應用程序 - > maste - >服務器 - >像那樣的供應商 – selvam

1

試試這個(這裏我用$injector注入SweetAlert

describe('Test Controller', function() { 
    var MainCtrl, 
     SweetAlert, 
     URLService, 
     httpcall, 
     scope; 
beforeEach(module('sample')); 
beforeEach(module('SweetAlert')); 

beforeEach(inject(function ($injector) { 
    $rootScope = $injector.get('$rootScope'); 
    $scope = $rootScope.$new(); 
    SweetAlert = $injector.get('SweetAlert'); 
    MainCtrl = $controller('TestController', { 
    $scope: scope, 
    httpcall : $http, 
    SweetAlert : SweetAlert 
    }); 
})); 
});