Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Injection of mocked service fails for nested service ... but only sometimes ... #8092

Open
ptandler opened this issue Jan 30, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@ptandler
Copy link

Description of the bug

We started using the very nice ng-mocks for our test, but I have trouble getting the mock injection right for this case:

  • OurService that uses the DatetimeService and NumberFormatterService.
  • NumberFormatterService also uses the DatetimeService.
  • DatetimeService should be mocked.

I also posted this issue on StackOverflow, but after realizing, that this problem happens only sometimes and quite randomly, I could imagine that this is a bug, so I decided to open an issue here. In fact, it could be a timing issue. (Or caching?)

An example of the bug

describe('OurService', () => {
  MockInstance.scope();

  let service: OurService;

  beforeEach(async () => {
    MockInstance(DatetimeService, 'locale', () => 'en-US');

    await MockBuilder(OurService)
      .mock(DatetimeService)
      .provide(NumberFormatterService);

    service = TestBed.inject(OurService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  test('test setup is correct', () => {
    expect(TestBed.inject(DatetimeService).locale()).toEqual('en-US'); // works
    expect(service.datetimeService.locale()).toEqual('en-US'); // works
    expect(service.numberFormatter.datetimeService.locale()).toEqual('en-US'); // <== this fails (sometimes): locale is undefined
  });
  
  // ...
});

Expected vs actual behavior

I would expect that the mocked Instance on DatetimeService is also always injected into the NumberFormatter.

@ptandler ptandler added the bug Something isn't working label Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants