Angular Karma Unit Test: How to inject a mock service in karma configuration instead of in all test specs

chfw

Recently, I have added a security feature to an existing angular app. Here is what I got afterwards:

Chrome 3X.0.2125 (Linux) ERROR
  Some of your tests did a full page reload!
Chrome 3X.0.2125 (Linux): Executed 23 of 102 (skipped 2) ERROR 

This is how I have set up the security feature:

angular.module('myapp', [/*..I have omitted..*/])
       .run(function(MyLoginSerivce, /*.. I have omitted ..*/)){
           if(!MyLoginService.isLoggedIn()){
               MyLoginService.redirectForLogin();
           }else{
               /* other logics */
           }
       }

I knew I have to add the following code to each and every test spec. But it sounds silly adding it to dozens of test files.

  beforeEach(module(function($provide){
    $provide.value("MyLoginServce", {
      isLoggedIn: function(){
        return true;
      },
      redirectForLogin: function {}
    });
  }));

Is there a way to tell Karma that use a mock service and add that piece of code only once and in one place?

Thanks

Current solution

Step 1: I saved this in a separate file, e.g. ./test/mocked.login.service.js:

   var mockedLoginService = {
      isLoggedIn: function(){
        return true;
      },
      redirectForLogin: function {}
    });

Step 2: I include the file in karma.conf.js, by inserting 'test/mocked.login.service.js'

Step 3: I just use it in my tests like the following:

  beforeEach(module(function($provide){
    $provide.value("MyLoginServce", mockedLoginService
  }));
pansay

You can extract the mocked service into a separate js file as an object, include that file in the karma.conf files list, then use that object as a global in your specs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to inject angular value and angular constant into karma unit test?

From Dev

mock angular service/promise in a karma/jasmine test

From Dev

Inject real implementation of Angular's $timeout service in a Karma/Jasmine unit test

From Dev

How to inject ngRoute into Jasmine / Karma AngularJS unit test?

From Dev

angularjs unit test karma with jasmine mock dilemma

From Dev

Angular 2 Observable Service Karma Jasmine Unit Test not working

From Dev

Jasmine unit test skipped by my Karma configuration

From Dev

Inject a module into karma test in Angular 1.5

From Dev

Using Jasmine/Karma To Unit Test A Service Won't Inject A Copy Of The Service

From Dev

Using Jasmine/Karma To Unit Test A Service Won't Inject A Copy Of The Service

From Dev

Debug a Karma unit test

From Dev

$routeUpdate not broadcast in angular karma unit test?

From Dev

angular karma jasmine unit test for a controller

From Dev

How to test 'private' functions in an angular service with Karma and Jasmine

From Dev

how to unit-test setInterval in karma angularjs

From Dev

Karma: how to unit test the filter with dependencies?

From Dev

Cannot inject providers into Karma test

From Dev

Unable to inject controller to Karma test

From Dev

Unit Test for Login with Jasmine and Karma

From Dev

Karma unit test fails in phantomjs

From Dev

How do I inject a mock dependency into an angular directive with Jasmine on Karma

From Dev

How do I inject a mock dependency into an angular directive with Jasmine on Karma

From Dev

Cannot inject service into its Angular unit test

From Dev

'Error: Unexpected request' during Karma Angular Unit Test

From Dev

Unit Test Karma Jasmine SyntaxError: Parse error on "&" Angular Directive binding

From Dev

Dependency injection not working in angular/browserify/karma/jasmine unit test

From Dev

How to share a mock between test files using karma?

From Dev

How can I unit test a promise chain with Karma?

From Dev

How to unit test React JSX ES6 code with KARMA?

Related Related

  1. 1

    How to inject angular value and angular constant into karma unit test?

  2. 2

    mock angular service/promise in a karma/jasmine test

  3. 3

    Inject real implementation of Angular's $timeout service in a Karma/Jasmine unit test

  4. 4

    How to inject ngRoute into Jasmine / Karma AngularJS unit test?

  5. 5

    angularjs unit test karma with jasmine mock dilemma

  6. 6

    Angular 2 Observable Service Karma Jasmine Unit Test not working

  7. 7

    Jasmine unit test skipped by my Karma configuration

  8. 8

    Inject a module into karma test in Angular 1.5

  9. 9

    Using Jasmine/Karma To Unit Test A Service Won't Inject A Copy Of The Service

  10. 10

    Using Jasmine/Karma To Unit Test A Service Won't Inject A Copy Of The Service

  11. 11

    Debug a Karma unit test

  12. 12

    $routeUpdate not broadcast in angular karma unit test?

  13. 13

    angular karma jasmine unit test for a controller

  14. 14

    How to test 'private' functions in an angular service with Karma and Jasmine

  15. 15

    how to unit-test setInterval in karma angularjs

  16. 16

    Karma: how to unit test the filter with dependencies?

  17. 17

    Cannot inject providers into Karma test

  18. 18

    Unable to inject controller to Karma test

  19. 19

    Unit Test for Login with Jasmine and Karma

  20. 20

    Karma unit test fails in phantomjs

  21. 21

    How do I inject a mock dependency into an angular directive with Jasmine on Karma

  22. 22

    How do I inject a mock dependency into an angular directive with Jasmine on Karma

  23. 23

    Cannot inject service into its Angular unit test

  24. 24

    'Error: Unexpected request' during Karma Angular Unit Test

  25. 25

    Unit Test Karma Jasmine SyntaxError: Parse error on "&" Angular Directive binding

  26. 26

    Dependency injection not working in angular/browserify/karma/jasmine unit test

  27. 27

    How to share a mock between test files using karma?

  28. 28

    How can I unit test a promise chain with Karma?

  29. 29

    How to unit test React JSX ES6 code with KARMA?

HotTag

Archive