-
Notifications
You must be signed in to change notification settings - Fork 86
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
Unit Testing: Set Up Question #47
Comments
@wtho Can you confirm what the solution to this was if a solution was found? |
Hey! When installing the spy, the testbed was already setup, and the component compiled and instantiated and the constructor already executed. The component lifecycle hooks, such as This means you have to call them in the test itself. You can call the lifecycle hooks on your own or make the test environment call them, e. g. an execution of I personally prefer to call the lifecycle hooks directly, as anyone can immediately see in your test, what you are testing. Modify your test in this way: it('should set the initial state object', () => {
const spy = spyOn(MockNgRedux.mockInstance, 'getState');
// alternatively call fixture.detectChanges(); here
component.ngOnInit();
expect(spy).toHaveBeenCalled();
}); Let us know if this solved your problem and feel free to close the issue if it did. Cheers! |
Hi. Thanks for the reply, that helped. I played around with things and got it to work with this setup: // In the main describe...
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ NgReduxTestingModule ],
declarations: [ MyComponent ],
schemas: [ NO_ERRORS_SCHEMA ]
})
.compileComponents();
MockNgRedux.reset();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
ngRedux = MockNgRedux.getInstance(); // mockInstance looks like it's deprecated
spyOn(ngRedux, 'getState');
fixture.detectChanges();
});
it('should set the initial state object', () => {
expect(ngRedux.getState).toHaveBeenCalled();
}); |
Posting this here, as well as in the old repo
This is a...
What toolchain are you using for transpilation/bundling?
ngc
Environment
NodeJS Version: 8.11.2
Typescript Version: 2.7.2
Angular Version: 6.0.9
@angular-redux/store version: 9.0.0
@angular/cli version: (if applicable)
Description
I've read through the unit test examples, and believe that I have my set up correct. However, it seems like my mock instance is not being substituted for the real
NgRedux
that is injected.Simplified Code
Component
Unit Test
Error Message
When I run my tests with the code above, I get the message:
Expected spy getState to have been called.
Am I missing something in my set up?
The text was updated successfully, but these errors were encountered: