Skip to content

A way to override instances of PrimordialInjector in unit tests #614

Closed

Description

@mhevery
  • Each tests needs to have a separate instance of primordial injector.
  • Delay creating the injector until we execute the it method
  • add bind method to the test vocabulary for overriding the injector.
main() {
  describe('some test', () => {
    bind(Type1);
    bind(Type2).toValue(...);
    bind(Type3).toFactory(...);
    bind(Type4).toClass(...);
    bind(Type5).toAsyncFactory(...);

    it('should do something', () => {});
  });
}

see DI

Activity

  1. added this to the M5: Early Adopters milestone on Feb 11, 2015
  2. vicb commented on Mar 11, 2015

    @vicb
    Contributor

    @mhevery this could probably not land before we switch to TS as there is no way to introspect the type of anonymous fn parameters.

    However I have started to work on it, could you please give feedback on the following (we can discuss about it on our next sync)

    describe('describe', () => {
    
      // - beforeEach would be executed after beforeEachBindings
      // - then we can imagine that we could inject classes in the cb
      // - there is a gotcha with nested beforeEachBindings because we can not create
      //   the injector before all beforeEachBindings have been executed 
      beforeEach((c: AClass, async: AsyncCompleter) => {
        // injectabe ?
      });
    
      // IMO it's easier to define the bindings in a function rather than to have
      // bind() available in describe().
      // As of now the function should return a list of bindings, we use the same
      // syntax as in non-test code.
      beforeEachBindings(() => [
        AClass,
        bind(Router).toClass(MockRouter)
      ]);
    
      // It is possible to inject into it() callback.
      // AsyncCompleter allow completing async tests (like done in jasmine)
      it('it', (class: AClass, router: Router, async: AsyncCompleter) => {
       router.route().then(() => { async.done(); });
      });
    
      describe(..., () => {
        // This should be executed before any BeforeEach (even in the parent describe)
        // if we want to be able to inject into BeforeEach callbacks.
        beforeEachBindings(() => [
          BClass,
        ]);
    
        it(...);
    
      });
    })
  3. angular-automatic-lock-bot commented on Sep 5, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  4. locked and limited conversation to collaborators on Sep 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions