zombiQWERTY
@zombiQWERTY
Фронтенд разработчик

Как запустить unit-тест?

Код тестируемого файла:
const Config = (stateHelperProvider, $urlRouterProvider, $locationProvider, $logProvider) => {
  /*@ngInject*/

  $logProvider.debugEnabled(true);
  $locationProvider.html5Mode(true);
  $urlRouterProvider.otherwise('/');
};

export default Config;


Вызов конфига в другом файле (в точке входа):

angular.module(appname, deps.concat(modules)).config(Config)


Код теста:
import angular from 'angular';
import 'angular-mocks';

describe('Configuration of application', () => {
  let $locationProvider;

  beforeEach(() => {
    angular.mock.module((_$locationProvider_) => {
      $locationProvider = _$locationProvider_;
      spyOn($locationProvider, 'html5Mode').and.callThrough();
    });

    angular.mock.module('Simply');
    inject();
  });

  it('should set html5 mode', () => {
    expect($locationProvider.html5Mode).toHaveBeenCalledWith(true);
  });
});


Код ошибки:
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 0 of 1 SUCCESS (0 secs / 0 secs)
PhantomJS 2.1.1 (Mac OS X 0.0.0) Configuration of application should set html5 mode FAILED
	forEach
	loadModules
	createInjector
	workFn
	Expected spy html5Mode to have been called with [ true ] but it was never called.
	stack@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1577:37
	buildExpectationResult@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1547:19
	expectationResultFactory@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:638:40
	addExpectationResult@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:330:58
	addExpectationResult@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:588:41
	/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1501:32
	attemptSync@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1886:28
	run@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1874:20
	execute@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1859:13
	queueRunnerFactory@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:697:42
	execute@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:359:28
	fn@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2479:44
	attemptAsync@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1916:28
	run@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1871:21
	execute@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1859:13
	queueRunnerFactory@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:697:42
	fn@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2464:31
	attemptAsync@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1916:28
	run@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1871:21
	execute@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1859:13
	queueRunnerFactory@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:697:42
	execute@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2326:25
	execute@/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:757:24
	/Users/zombiQWERTY/WebstormProjects/project/node_modules/karma-jasmine/lib/adapter.js:331:23
	loaded@http://localhost:9876/karma.js:194:17
	global code@http://localhost:9876/context.html:42:28
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs / 0.013 secs)
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.002 secs / 0.013 secs)


Karma config:

// Reference: http://karma-runner.github.io/0.12/config/configuration-file.html
module.exports = function karmaConfig (config) {
  config.set({
    // Log our errors
    logLevel: config.LOG_DEBUG,

    frameworks: [
      // Reference: https://github.com/karma-runner/karma-jasmine
      // Set framework to jasmine
      'jasmine'
    ],

    reporters: [
      // Reference: https://github.com/mlex/karma-spec-reporter
      // Set reporter to print detailed results to console
      'progress',

      // Reference: https://github.com/karma-runner/karma-coverage
      // Output code coverage files
      'coverage'
    ],

    files: [
      // Grab all files in the app folder that contain .spec.
      './src/*.spec.js',
      './src/**/*.spec.js'
      // each file acts as entry point for the webpack configuration
    ],

    preprocessors: {
      // Reference: http://webpack.github.io/docs/testing.html
      // Reference: https://github.com/webpack/karma-webpack
      // Convert files with webpack and load sourcemaps
      './src/*.spec.js': ['webpack', 'sourcemap'],
      './src/**/*.spec.js': ['webpack', 'sourcemap']
    },

    browsers: [
      // Run tests using PhantomJS2
      'PhantomJS2'
    ],

    singleRun: true,

    // Configure code coverage reporter
    coverageReporter: {
      dir: 'coverage/',
      reporters: [
        {type: 'text-summary'},
        {type: 'html'}
      ]
    },

    webpack: require('./webpack.config'),

    // Hide webpack build information from output
    webpackMiddleware: {
      noInfo: 'errors-only'
    }
  });
};


Все завязано на вебпаке
  • Вопрос задан
  • 400 просмотров
Решения вопроса 1
ruddy22
@ruddy22
Спасение утопающих — дело рук самих утопающих
У Вас в ошибке сказано, что ожидается вызов с параметром true, но вызова не происходит.
Значит нужно перед expect произвести вызов функции.

describe('SomeBlock.init()', function () {
      beforeEach(function () {
        spyOn(SomeBlock, 'init').and.callThrough();
        spyOn(SomeBlock, 'addTab');
        SomeBlock.init();
      });
      it('SomeBlock.init() have been called', function () {
        expect(SomeBlock.init).toHaveBeenCalled();
      });
      it('SomeBlock.init() invoke addTab', function () {
        expect(SomeBlock.addTab).toHaveBeenCalled();
      });
      it('SomeBlock.init() invoke addTab gt 0 times', function () {
        expect(SomeBlock.addTab.calls.count()).toBeGreaterThan(0);
      });
    });
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы