Sasxa
Sasxa

Reputation: 41284

Changing provider in tests using ReflectiveInjector

Is it possible to change provider using ReflectiveInjector in the same context? I need B to use different value in second test. A & B are services...

  let injector: ReflectiveInjector;
  beforeEach(() => {
    injector = ReflectiveInjector.resolveAndCreate([
      A, { provide: B, useValue: null }
    ]);
  });

  ...

  it('should be null', () => {
    const a = injector.get(A);
    expect(a.B).toBe(null)
  });

  it('shoul NOT be null', () => {
    const a = injector.get(A);
    // need something like this:
    // a.B = injector.CHANGE_B_PLEASE({ provide: B, useValue: true })
    expect(a.B).not.toBe(null)
  });

Upvotes: 0

Views: 261

Answers (1)

Sasxa
Sasxa

Reputation: 41284

My solution for testing:

  let injector: ReflectiveInjector;
  setup((value) => {
    injector = ReflectiveInjector.resolveAndCreate([
      A, { provide: B, useValue: value }
    ]);
  });

  ...

  it('should be null', () => {
    setup(null);
    const a = injector.get(A);
    expect(a.B).toBe(null)
  });

  it('shoul NOT be null', () => {
    setup(true);
    const a = injector.get(A);
    const b = injector.get(B);
    expect(a.B).toBe(true)
    expect(b).toBe(true)
  });

For more information check:

Upvotes: 1

Related Questions