-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
MockRender is not working when we try to spy on a service method which is called in component constructor #488
Comments
Hi again, Thanks for the report. This is expected. Because MockRender adds a middleware component. To spy in such cases MockInstance should be used. Please don't close the ticket. I'll update docs based on the issue. |
@satanTime Can we add a configurable option to bypass the middleware component? add it only when needed? something like |
Something like that. describe("mock extended component", () => {
beforeEach(() => {
MockInstance(AppService, 'method', jasmine.createSpy('method'));
//
// Or simply ngMocks.autoSpy('jasmine');
});
beforeEach(() => MockBuilder(AppComponent, AppModule));
it("expect service.method to have been called", () => {
MockRender(AppComponent);
const service = TestBed.get(AppService);
expect(service.method).toHaveBeenCalled();
});
}); If you want to bypass the middleware component it means you don't need The idea of |
Thanks for the explanation 👍 |
You are welcome, feel free to ask any questions and propose any changes. I'll dive deeper and check if it is possible somehow to inject a component after the initialization and post an update here. The main diff between Might I ask to explain why you want to keep |
Here is the snippet from our codebase. configureModuleBuilder(() => {
return MockBuilder(XComponent, XModule)
.mock(DomSanitizer, { bypassSecurityTrustUrl: v => v }, { precise: true })
.provide([
{ provide: I18NService, useClass: I18NServiceMock },
{ provide: WindowService, useClass: WindowMock },
{ provide: Store, useClass: StoreMock },
XActions,
]);
});
beforeEach(() => {
mockWindowService = TestBed.inject(WindowService);
store = TestBed.inject(Store);
spyOn(store, 'dispatch');
fixture = TestBed.createComponent(XComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('dispatches ', () => {
expect(store.dispatch).toHaveBeenCalledWith();
}); When I tried to use It will be good if we are in the process of migrating all our testcases to use |
Oh, it looks a bit confusing. Might you share how |
Sorry. Can't share the code for these. |
Sure, I totally understand this. I'll try to find a way how to make your test working and being something like: configureModuleBuilder(() => {
MockInstance(DomSanitizer, 'bypassSecurityTrustUrl', v => v);
MockInstance(I18NService, I18NServiceMock);
MockInstance(WindowService, WindowMock);
MockInstance(Store, StoreMock);
MockInstance(Store, store => spyOn(store, 'dispatch'));
return MockBuilder(XComponent, XModule).provide(XActions);
});
beforeEach(() => {
fixture = MockRender(XComponent);
component = fixture.point.componentInstance;
mockWindowService = TestBed.inject(WindowService);
store = TestBed.inject(Store);
});
it('dispatches ', () => {
expect(store.dispatch).toHaveBeenCalledWith();
}); |
Thank for your time on this. |
Could you create a separate issue and provide an example how you want it to memoize the things? |
beforeEach(() => {
MockInstance(Store, 'dispatch', jasmine.createSpy());
MockInstance(WindowService, 'open', jasmine.createSpy());
mockWindowService = TestBed.inject(WindowService);
store = TestBed.inject(Store);
fixture = MockRender(XComponent);
component = fixture.componentInstance;
}); This snippet is not working. |
Sure. will do. |
Hi, the snippet you provided won't work, because calls of I think, I understood the point, which is missing in all examples above, this is incorrect usage of tools:
To make the example above work properly, its test should look like that: // simplifies spy configuration
beforeEach(() => ngMocks.autoMock('jasmine'));
// beforeEach(() => MockInstance(Store, 'dispatch', jasmine.createSpy()));
// beforeEach(() => MockInstance(WindowService, 'open', jasmine.createSpy()));
// describing environment
beforeEach(() => MockBuilder(XComponent).mock(Store).mock(WindowService));
// initializing testing environment
beforeEach(() => {
fixture = MockRender(XComponent);
mockWindowService = TestBed.inject(WindowService);
store = TestBed.inject(Store);
});
it('test', () => {
expect(store.dispatch).toHaveBeenCalled();
}); For the mock classes, could you try like that and let me know if it works? configureModuleBuilder(() => {
return MockBuilder(XComponent, XModule)
.provide([
MockProvider(DomSanitizer, { bypassSecurityTrustUrl: v => v }),
MockProvider(I18NService, new I18NServiceMock()),
MockProvider(WindowService, new WindowMock()),
MockProvider(Store, ngMocks.stub(new StoreMock(), {
dispatch: jasmine.createSpy(), // a way to provide a spy before call of MockRender
})),
XActions,
]);
}); |
feat(mock-render): throws on wrong usage #488
feat(faster): supports MockRender in beforeAll #488
v11.11.0 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems. |
Hi @vinayk406, I've added an option to memories |
And this is info about default values in params: https://ng-mocks.sudo.eu/api/MockRender#params-inputs-and-outputs |
Stackblitz link to reproduce this issue
https://stackblitz.com/edit/ng-mocks-examples-gwee2q?file=app/app.component.spec.ts
The text was updated successfully, but these errors were encountered: