具有SweetAlert依赖性

时间:2016-12-08 14:58:29

标签: angularjs unit-testing dependency-injection karma-jasmine sweetalert

我想用karma和jasmine对测试控制器进行单元测试。它一直有效,直到我将SweetAlert添加到控制器。

这是测试控制器:

(function () {
    'use strict';

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

    TestController.$inject = ['$scope', 'SweetAlert'];

    function TestController($scope, SweetAlert) {
        $scope.greeting = 'Hello world!';
    }

})();

这是我的karma.conf.js:

var relBaseFiles = require('../vendor.base.json').map(function(path) {
    return '../' + path;
});

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: [].concat(
            require('../vendor.base.json'),
            '../app/js/app.js',
            './bower_components/angular/angular.js',
            './bower_components/angular-mocks/angular-mocks.js',
            './bower_components/sweetalert/dist/sweetalert.css',
            './bower_components/sweetalert/dist/sweetalert.min.js',
            './bower_components/angular-sweetalert/SweetAlert.js',
            './tests/unit/*.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: true,


        // start these browsers
        // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
        browsers: ['PhantomJS'],


        // 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:

describe('Test Controller example', function () {

    beforeEach(module('SweetAlert'));
    beforeEach(module('app.testController'));
    var TestController, scope, sweetAlert;

    beforeEach(inject(function ($rootScope, $controller, _SweetAlert_) {
        scope = $rootScope.$new();
        sweetAlert = _SweetAlert_;
        TestController = $controller('TestController', {
            $scope: scope,
            SweetAlert: sweetAlert
        });
    }));

    it('says Hello World', function () {
        expect(scope.greeting).toEqual('Hello world!');
    });
});

这是我得到的错误:

08 12 2016 15:54:45.237:INFO [karma]: Karma v1.3.0 server started at http://localhost:9876/
08 12 2016 15:54:45.239:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
08 12 2016 15:54:45.244:INFO [launcher]: Starting browser PhantomJS
08 12 2016 15:54:45.432:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket /#xDRclhF-qRPbB1QIAAAA with id 6741793
PhantomJS 2.1.1 (Linux 0.0.0) Test Controller example says Hello World FAILED
    forEach@bower_components/angular/angular.js:321:24
    loadModules@bower_components/angular/angular.js:4487:12
    createInjector@bower_components/angular/angular.js:4409:30
    WorkFn@bower_components/angular-mocks/angular-mocks.js:3088:60
    loaded@http://localhost:9876/context.js:151:17
    bower_components/angular/angular.js:4527:53
    TypeError: undefined is not an object (evaluating 'scope.greeting') in tests/unit/test.spec.js (line 18)
    tests/unit/test.spec.js:18:21
    loaded@http://localhost:9876/context.js:151:17
PhantomJS 2.1.1 (Linux 0.0.0): Executed 2 of 2 (1 FAILED) (0.044 secs / 0.006 secs)
[15:54:45] Karma completed
[15:54:45] 'test:unit' errored after 456 ms
[15:54:45] Error: 
********************************
karma: tests failed with code 1
********************************
    at formatError (/usr/lib/node_modules/gulp/bin/gulp.js:169:10)
    at Gulp.<anonymous> (/usr/lib/node_modules/gulp/bin/gulp.js:195:15)
    at emitOne (events.js:96:13)
    at Gulp.emit (events.js:188:7)
    at Gulp.Orchestrator._emitTaskDone (/home/nidzo/Day4ERP/master/node_modules/orchestrator/index.js:264:8)
    at /home/nidzo/Day4ERP/master/node_modules/orchestrator/index.js:275:23
    at finish (/home/nidzo/Day4ERP/master/node_modules/orchestrator/lib/runTask.js:21:8)
    at cb (/home/nidzo/Day4ERP/master/node_modules/orchestrator/lib/runTask.js:29:3)
    at karmaCompleted (/home/nidzo/Day4ERP/master/gulpfile.js:518:13)
    at removeAllListeners (/home/nidzo/Day4ERP/master/node_modules/karma/lib/server.js:379:7)
    at Server.<anonymous> (/home/nidzo/Day4ERP/master/node_modules/karma/lib/server.js:390:9)
    at Server.g (events.js:291:16)
    at emitNone (events.js:91:20)
    at Server.emit (events.js:185:7)
    at emitCloseNT (net.js:1555:8)
    at _combinedTickCallback (internal/process/next_tick.js:71:11)

我坚持下去。感谢您的帮助。

0 个答案:

没有答案