【问题标题】:AngularJS + Karma + Jasmine: beforeEach() - angular.module vs angular.mock.moduleAngularJS + Karma + Jasmine: beforeEach() - angular.module vs angular.mock.module
【发布时间】:2018-04-20 20:01:02
【问题描述】:
  • 我是 Jasmine 和 Karma 测试的新手。
  • 我正在尝试对 AngularJs 服务进行单元测试。
  • 在编写 Specs 时,我发现了两种用于注入模块的代码。

第一种

beforeEach(angular.mock.module("app"));

第二类

beforeEach(function () {
    angular.module("app");
});
  • 谁能用一个简单的例子用简单的英语解释以上两者之间的区别。
  • 另外,在什么样的场景中使用哪个代码?
  • 我尝试用 Google 搜索,但找不到正确答案。

谢谢:)

【问题讨论】:

    标签: angularjs karma-jasmine


    【解决方案1】:

    来自 angular-mocks.js

    window.module = angular.mock.module = function() {
    ...
    

    更多信息:

    http://www.bradoncode.com/blog/2015/05/24/ngmock-fundamentals-angularjs-unit-testing/ https://github.com/bbraithwaite/angular.js/blob/master/src/ngMock/angular-mocks.js#L2259 https://gist.github.com/trinitroglycerin/68754b920df83a977f12#gistcomment-2851474

    要启用“模块”的使用,请将其添加到 webpack 配置中:

    const webpack = require('webpack');
    
    module.exports = function(config) {
      config.set({
        //...config
        webpack: {
          //...webpack config
          plugins: [
            new webpack.DefinePlugin({
              module: 'window.module',
            }),
          ],
        }
      });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-04
      • 2013-12-20
      • 1970-01-01
      • 2014-06-06
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多