无法加载angular.mock.module

时间:2017-05-30 05:53:39

标签: angularjs karma-jasmine

我目前正在学习编写单元测试,我正在尝试为Ui-Router Sample app进行一些单元测试。

不幸的是, angular.mock.module 不会加载主模块。

由于打字稿 browserify ,因此karma可以正确读取文件。

这是错误:

/**
 * This file imports the third party library dependencies, then creates the angular module "demo"
 * and exports it.
 */

// External dependencies
import * as angular from "angular";
import uiRouter from "@uirouter/angularjs";
import {visualizer} from "@uirouter/visualizer";

// Create the angular module "demo".
//
// Since it is exported, other parts of the application (in other files) can then import it and register things.
// In bootstrap.js, the module is imported, and the components, services, and states are registered.
export const ngmodule = angular.module("demo", [uiRouter]);

// Show ui-router-visualizer
ngmodule.run($uiRouter => visualizer($uiRouter));

const BLANK_MODULE = {
  states: [],
  components: {},
  directives: {},
  services: {},
  filters: {},
  configBlocks: [],
  runBlocks: []
};

/**
 * Register each app module's states, directives, components, filters, services,
 * and config/run blocks with the `ngmodule`
 *
 * @param ngModule the `angular.module()` object
 * @param appModule the feature module consisting of components, states, services, etc
 */
export function loadNg1Module(ngModule, appModule) {

  let module = Object.assign({}, BLANK_MODULE, appModule);


  ngModule.config(['$stateProvider', $stateProvider => module.states.forEach(state => $stateProvider.state(state))]);

  Object.keys(module.components).forEach(name => ngModule.component(name, module.components[name]));

  Object.keys(module.directives).forEach(name => ngModule.directive(name, module.directives[name]));

  Object.keys(module.services).forEach(name => ngModule.service(name, module.services[name]));

  Object.keys(module.filters).forEach(name => ngModule.filter(name, module.filters[name]));

  module.configBlocks.forEach(configBlock => ngModule.config(configBlock));

  module.runBlocks.forEach(runBlock => ngModule.run(runBlock));

  return ngModule;
}

应用/引导/ ngmodule.js

describe('Angular Main Module', function() {

  var AuthService;

  beforeEach(angular.mock.module('demo'));

  beforeEach(inject(function(_AuthService_) {
    AuthService = _AuthService_;
  }));

  it('should exist', function() {
    expect(AuthService).toBeDefined();
  });


})

测试/ ngmodule.spec.js

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', 'browserify'],
    files: [
      './node_modules/angular/angular.js',
      './node_modules/@uirouter/angularjs/release/angular-ui-router.js',
      './node_modules/@uirouter/visualizer/bundles/visualizer.min.js',
      './node_modules/angular-mocks/angular-mocks.js',
      {pattern: './transpiled/app/**/*.map.js', included: true, watched: true, served: true},
      {pattern: './tests/**/*.js', included: true, watched: true, served: true}
    ],
    exclude: [
    ],
    preprocessors: {
      'transpiled/app/**/*.map.js': [ 'browserify' ]
    },
    reporters: ['spec'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    concurrency: Infinity
  })
}

karma.conf.js

{
  "name": "ui-router-architecture",
  "description": "Architecture overview demo for Angular UI-Router",
  "version": "1.0.2",
  "scripts": {
    "start": "npm run watch:compile & npm run serve",
    "compile": "./node_modules/typescript/bin/tsc",
    "watch:compile": "./node_modules/typescript/bin/tsc -w",
    "serve": "lite-server",
    "build": "babel app -d lib",
    "gh-pages": "git checkout gh-pages && git rebase master && rm -rf node_modules && npm install && echo 'If there are any updated modules:' && echo 'git commit -am update_libs' && echo 'git rebase -i' && echo && echo 'Finally:' && echo 'git push origin gh-pages:gh-pages --force' && echo 'git checkout master'"
  },
  "contributors": [
    {
      "name": "Chris Thielen",
      "web": "https://github.com/christopherthielen"
    }
  ],
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/ui-router/sample-app.git"
  },
  "dependencies": {
    "@uirouter/visualizer": "^4.0.0",
    "@uirouter/angularjs": "^1.0.0",
    "angular": "1.5.6",
    "bootstrap": "^3.3.6",
    "d3": "^3.5.16",
    "font-awesome": "^4.5.0",
    "systemjs": "^0.19.24",
    "systemjs-plugin-css": "^0.1.20",
    "typescript": "^1.8.7"
  },
  "devDependencies": {
    "angular-mocks": "1.5.6",
    "babel-cli": "^6.24.1",
    "babel-preset-env": "^1.5.1",
    "browserify": "^14.4.0",
    "jasmine-core": "^2.4.1",
    "karma": "^0.13.22",
    "karma-browserify": "^5.1.1",
    "karma-chrome-launcher": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-spec-reporter": "0.0.26",
    "lite-server": "^2.2.2",
    "watchify": "^3.9.0"
  }
}

的package.json

www.example.com/amp/amp-list.html

0 个答案:

没有答案