Angular 2: How to mock ChangeDetectorRef while unit testing

Update 2020: I wrote this originally in May 2017, it’s a solution that worked great at the time and still works. We can’t configure the injection of a changeDetectorRef mock through the test bed, so this is what I am doing these days: it(‘detects changes’, () => { // This is a unique instance here, … Read more

Unit testing angular 5 component with @ViewChild

You can do something like this. Create a spy object for the ChildComponent like this. const childComponent = jasmine.createSpyObj(‘ChildComponent’, [‘childMethod’]); Then in the test, set the component’s childComponent property to the spy that you have created. component.childComponent = childComponent; Your test file should look like this. import { NO_ERRORS_SCHEMA } from “@angular/core”; import { ComponentFixture, … Read more

Angular 4 – Failed: Can’t resolve all parameters for ActivatedRoute: (?, ?, ?, ?, ?, ?, ?, ?)

You want to inject a fake ActivatedRoute to your component, since you create it yourself in the test, and the router thus doesn’t create it for you and inject an ActivatedRoute. So you can use something like this: describe(‘SomeComponent’, () => { const fakeActivatedRoute = { snapshot: { data: { … } } } as … Read more

What is the difference between testbed.get and inject in Angular 2/Jasmine testing?

Just to add to the existing answer and if like me you found this question because you are wondering what the difference is between TestBed.get() and TestBed.inject() which I know was not quite what the OP originally asked but it is relevant and is very much related. I thought it was worth posting that according … Read more

How override Provider in Angular 5 for only one test?

As of angular 6 I noticed that overrideProvider works with the useValue property. So in order to make it work try something like: class MockRequestService1 { … } class MockRequestService2 { … } then write you TestBed like: // example with injected service TestBed.configureTestingModule({ // Provide the service-under-test providers: [ SomeService, { provide: SomeInjectedService, useValue: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)