【问题标题】:Angular module test not working角度模块测试不起作用
【发布时间】:2015-07-05 16:57:19
【问题描述】:

我正在尝试测试一个 angularjs 应用程序。我设法在我的笔记本电脑上运行了一些基本测试。这些测试只是检查一个模块并检查一些基本功能。这在我的笔记本电脑上运行良好。但是,当我将测试环境移动到本地服务器时,测试停止工作。例如,无论我在测试中通过什么模块名称仍然通过。我还没有尝试测试任何功能。只是一个模块测试。

app.js:

var app = angular.module('mcqsApp', []);

simpleTest.js:

describe('SimpleService test', function(){
    beforeEach(function(){
        module('blah');
    });

    it('SimpleService should have a module',function(){

    });
});

karma.conf.js:

module.exports = function(config){
config.set({
    //  root path location that will be used to resolve all relative paths in files and exclude sections, should be the root of your project
    basePath : '../',

    // files to include, ordered by dependencies
    files : [
        // include relevant Angular files and libs
        'https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js',
        'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-mocks.js',

        // include js files
        '../../../public/js/app.js',

        '../../../public/js/services/*/*.js',

        // include unit test specs
        'test/unit/services/*.js'
    ],
    // files to exclude
    exclude : [

    ],

    // karma has its own autoWatch feature but Grunt watch can also do this
    autoWatch : true,

    // testing framework, be sure to install the karma plugin
    frameworks: ['mocha', 'chai'],

    // browsers to test against, be sure to install the correct karma browser launcher plugin
    browsers : ['PhantomJS'],

    // progress is the default reporter
    reporters: ['mocha'],

    // map of preprocessors that is used mostly for plugins
    preprocessors: {

    },

    // list of karma plugins
    plugins : [
        'karma-junit-reporter',
        'karma-chrome-launcher',
        'karma-firefox-launcher',
        'karma-mocha',
        'karma-chai',
        'karma-sinon',
        'karma-mocha-reporter',
        'karma-phantomjs-launcher'
    ]
})}

据我了解,如果模块名称不匹配,这应该会失败。但是无论模块名称如何,此测试都会通过。任何帮助将不胜感激。如果你们需要查看更多代码,例如我的 karma.conf.js 文件,请询问,我会进行更新。

【问题讨论】:

  • 你的 karma.conf 会很好
  • 用 karma.conf 更新
  • 首先要解决的问题:对 angular 和 angular-mocks 使用相同的版本。
  • @JBNizet 现在完成了,它们都是 1.3.15。仍然传递了一个不正确的模块。

标签: angularjs unit-testing mocha.js karma-runner


【解决方案1】:

问题原来是我没有将适当的 bower_components 加载到 karma.conf.js 中。更改为未缩小版本的 Angular 让我看到了帮助我解决这个问题的更具描述性的错误。

【讨论】:

    猜你喜欢
    • 2017-11-29
    • 2018-09-11
    • 2018-06-07
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    相关资源
    最近更新 更多