ngMock.angular.mock.module.sharedInjector

Improve this Doc View Source angular.mock.module.sharedInjector

  1. function in module ngMock

NOTE: This function is declared ONLY WHEN running tests with jasmine or mocha

This function ensures a single injector will be used for all tests in a given describe context. This contrasts with the default behaviour where a new injector is created per test case.

Use sharedInjector when you want to take advantage of Jasmine's beforeAll(), or mocha's before() methods. Call module.sharedInjector() before you setup any other hooks that will create (i.e call module()) or use (i.e call inject()) the injector.

You cannot call sharedInjector() from within a context already using sharedInjector().

Typically beforeAll is used to make many assertions about a single operation. This can cut down test run-time as the test setup doesn't need to be re-run, and enabling focussed tests each with a single assertion.

describe("Deep Thought", function() {

  module.sharedInjector();

  beforeAll(module("UltimateQuestion"));

  beforeAll(inject(function(DeepThought) {
    expect(DeepThought.answer).toBeUndefined();
    DeepThought.generateAnswer();
  }));

  it("has calculated the answer correctly", inject(function(DeepThought) {
    // Because of sharedInjector, we have access to the instance of the DeepThought service
    // that was provided to the beforeAll() hook. Therefore we can test the generated answer
    expect(DeepThought.answer).toBe(42);
  }));

  it("has calculated the answer within the expected time", inject(function(DeepThought) {
    expect(DeepThought.runTimeMillennia).toBeLessThan(8000);
  }));

  it("has double checked the answer", inject(function(DeepThought) {
    expect(DeepThought.absolutelySureItIsTheRightAnswer).toBe(true);
  }));

});

© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://code.angularjs.org/1.5.11/docs/api/ngMock/function/angular.mock.module.sharedInjector

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部