I've a component Strip
. It is a simple card in HTML with some numbers and calculation. Here's the code:
import { Component, Input, NgZone, OnInit } from '@angular/core';
import './strip.component.scss';
import { SisenseService } from '../../core/services/sisense.service';
@Component({
selector: 'app-strip',
templateUrl: './strip.component.html'
})
export class StripComponent extends BaseCardComponent implements OnInit {
constructor(private zone: NgZone, private sisenseService: SisenseService) {
super();
}
ngOnInit(): void {
}
}
I'm injecting 2 services, SisenseService
and NgZone
.
I'm writing a very simple test case. Here's the spec file:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { StripComponent } from './strip.component';
import { CUSTOM_ELEMENTS_SCHEMA, NgZone } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';
import { SisenseService } from '../../core/services/sisense.service';
describe('StripComponent', () => {
let component: StripComponent;
let fixture: ComponentFixture<StripComponent>;
let sisenseService: SisenseService;
let ngZone: NgZone;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [StripComponent],
imports: [TranslateModule.forRoot()],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers: [SisenseService, NgZone]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(StripComponent);
component = fixture.componentInstance;
fixture.detectChanges();
sisenseService = TestBed.get(SisenseService);
ngZone = TestBed.get(NgZone);
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
But it is failing telling me that:
Chrome Headless 76.0.3803.0 (Windows 10) StripComponent should create FAILED Failed: Can't resolve all parameters for NgZone: (?). Error: Can't resolve all parameters for NgZone: (?).
Please point out my mistake.
Try to mock the NgZone and use that in the providers
const mockNgZone = jasmine.createSpyObj('mockNgZone', ['run', 'runOutsideAngular']);
mockNgZone.run.and.callFake(function () {return undefined;});
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [StripComponent],
imports: [TranslateModule.forRoot()],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers: [
SisenseService,
{provide: NgZone, useValue: mockNgZone}
]
}).compileComponents();
}));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.