P
P
Pavel Zinoviev2016-02-29 05:18:57
JavaScript
Pavel Zinoviev, 2016-02-29 05:18:57

How to run a unit test?

Tested file code:

const Config = (stateHelperProvider, $urlRouterProvider, $locationProvider, $logProvider) => {
  /*@ngInject*/

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

export default Config;

Calling the config in another file (at the entry point):
angular.module(appname, deps.concat(modules)).config(Config)

Test code:
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);
  });
});

Error code:
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.
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1577:37
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1547:19
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:638:40
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:330:58
  [email protected]/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
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1886:28
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1874:20
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1859:13
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:697:42
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:359:28
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2479:44
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1916:28
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1871:21
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1859:13
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:697:42
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2464:31
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1916:28
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1871:21
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1859:13
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:697:42
  [email protected]/Users/zombiQWERTY/WebstormProjects/project/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2326:25
  [email protected]/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
  [email protected]://localhost:9876/karma.js:194:17
  global [email protected]://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'
    }
  });
};

It's all about webpack

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey P, 2016-02-29
@zombiQWERTY

Your error says that a call with the true parameter is expected, but the call does not occur.
So you need to call the function before 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);
      });
    });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question