Skip to content

feat(test): ability to configure providers on a per-spec basis #5128

Closed

Description

@btford

Right now, the only way to configure DI in tests is to use beforeEachProvider. I need the ability to configure providers on a per-spec basis for routing (currently I have to create extraneous describe blocks to overcome this limitation).

The inject helper could be extended to include this functionality, for instance.

Activity

  1. added
    area: testingIssues related to Angular testing features, such as TestBed
    on Nov 4, 2015
  2. self-assigned this
    on Nov 4, 2015
  3. juliemr commented on Nov 19, 2015

    @juliemr
    Contributor

    Syntax proposal:

    it('should set up a provider for just one test', inject([A, B], (a, b) => {
      // do stuff
    }, () => [provide(A, {useValue: 'a'}), provide(B, {useClass: BImpl})]));

    or

    it('should set up a provider for just one test', withProviders(
      () => [provide(A, {useValue: 'a'}), provide(B, {useClass: BImpl})]
    ).inject([A, B], (a, b) => {
      // do stuff
    }));

    Thoughts? I'm partial to option 2 because the providers come first and it's more explicit what they're doing.

  4. btford commented on Nov 19, 2015

    @btford
    ContributorAuthor

    I also like option 2

  5. juliemr commented on Nov 20, 2015

    @juliemr
    Contributor

    An additional thing to think about: eventually, when TS supports it, we'd like to be able to do:

    it("should do stuff", (@Inject() someInjectable: SomeInjectable) => {
    });

    I'm not sure if there'd be a nice way to fit per-spec providers in to that.

  6. btford commented on Nov 21, 2015

    @btford
    ContributorAuthor

    I can think of several ways to add providers there, but none of them stick out as particularly nice. :/

  7. juliemr commented on Dec 2, 2015

    @juliemr
    Contributor

    What about:

    // For now
    it('should set up a provider for just one test', withProviders(
      () => [provide(A, {useValue: 'a'}), provide(B, {useClass: BImpl})]
    ).inject([A, B], (a, b) => {
      // do stuff
    }));
    
    // In the future of TS
    it('should set up provider for just one test', withProviders(
      () => [provide(A, {useValue: 'a'}), provide(B, {useClass: BImpl})]
    ).run(@Inject(A) a, @Inject(B) b) => {
     // do stuff
    }));
  8. btford commented on Dec 3, 2015

    @btford
    ContributorAuthor

    Works for me. I'm not too picky about the implementation.

    On Wed, Dec 2, 2015, 15:13 Julie Ralph [email protected] wrote:

    What about:

    // For now

    it('should set up a provider for just one test', withProviders(
    () => [provide(A, {useValue: 'a'}), provide(B, {useClass: BImpl})]
    ).inject([A, B], (a, b) => {
    // do stuff
    }));

    // In the future of TSit('should set up provider for just one test', withProviders(
    () => [provide(A, {useValue: 'a'}), provide(B, {useClass: BImpl})]
    ).run(@Inject(A) a, @Inject(B) b) => {
    // do stuff
    }));

    —

    Reply to this email directly or view it on GitHub
    #5128 (comment).

  9. angular-automatic-lock-bot commented on Sep 8, 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.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions