簡體   English   中英

如何使用業力在測試文件之間共享模擬?

[英]How to share a mock between test files using karma?

我正在一個角度應用程序中工作,其中許多測試文件共享相同的模擬。 我想知道如何提取這些模擬並將它們放在一個單獨的文件中。

我試圖創建一個對象並在我的測試中重用它,但我得到了一個異常: ReferenceError: Can't find variable: require

業力文件:

module.exports = {
  options: {
    frameworks: ['jasmine'],
    preprocessors: {
      '<%= paths.scripts %>/**/*.js': ['coverage']
    },
    reporters: ['progress', 'coverage', 'junit'],
    coverageReporter: {
      dir : '<%= paths.coverage %>',
      reporters: [
        { type: 'html', subdir: 'report-html' },
        { type: 'cobertura', subdir: '.', file: 'code-coverage.xml' },
      ]
    },
    junitReporter: {
      outputFile: '<%= paths.testReport %>/test-results-karma.xml'
    },
    port: 9999,
    colors: true,
    logLevel: 'INFO',
    autoWatch: false,
    browsers: ['PhantomJS'],
    singleRun: true
  },

  portal: {
    options: {
      basePath: 'public/',
      files: [
        'bower_components/angular/angular.js',
        'bower_components/angular-mocks/angular-mocks.js',
        'bower_components/angular-sanitize/angular-sanitize.js',
        'bower_components/angular-bootstrap/ui-bootstrap.js',
        'bower_components/angular-translate/angular-translate.js',
        'bower_components/angular-translate-loader-static-files/angular-translate-loader-static-files.js',
        'scripts/<%= paths.module %>',
        'scripts/**/*.js',
        'test/mock/**/*.js',
        'test/spec/unit/**/*.js'
      ]
    }
  }
};

嘲笑:

var EcMock = (function() {
  'use strict';

  function ecMock() {
    this.dom = {
      addClass: function() {
        return angular.noop();
      },
      getViewportWidth: function() {
        return angular.noop();
      }
    };

    this.detect = {
      geolocation: true
    };
  }

  return ecMock;
})();

module.exports = EcMock;

在業力測試文件中的用法:

var EcMock = require('../../../../mock/EcMock');  

我想出了如何以角度方式解決這個問題。 評論正在解釋你必須做什么。

 //First step: Make sure you included your mock files in the karm conf file //Second step: Define a mock service (function() { 'use strict'; angular.module('mock') .factory('myServiceMock', myServiceMock); function myServiceMock($q) { var methodsToMock = ['myMethod1', 'myMethod2'], service = {}; //Create an object with spies already on it. Use one of the approaches below: //Approach 1: simple methods service = jasmine.createSpyObj('myServiceMock', methodsToMock]); //Approach 2: promise methods angular.forEach(methodsToMock, function(method) { service[method] = jasmine.createSpy(method).and.returnValue($q.when({})); }); return service; } })(); //Third step: Mock service usage (function() { 'use strict'; describe('Service Test', function() { var myServiceMock; //Load mock module beforeEach(module('mock')); beforeEach(inject(function(_myServiceMock_) { //Get mock service myServiceMock = _myServiceMock_; })); describe('Feature Test', function() { it('should test something', function() { //You can return whatever you want from your mock method myServiceMock.method1.and.returnValue('Return some value'); }); }); }); })(); //Remember you have to tell angular to use your mock service instead of your real one. //If you're testing a controller: beforeEach(inject(function(_$controller_, _myServiceMock_) { scope = _$rootScope_.$new(); myServiceMock = _myServiceMock_; MyController = _$controller_('MyController', { $scope: scope, myServiceMock: myServiceMock //Use mock service instead of real one }); })); //If you're testing another service: beforeEach(function () { module(function($provide) { $provide.service('myService', myServiceMock); }); });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM