angular - Angular9 单元测试:是否可以模拟 Parse.initialize 并且仍然有可靠的测试?
问题描述
我想测试组件如何使用 Parse.initialize(appId,url) 函数在我的Angular Service
?
这是初始化部分:
Parse.initialize('MY_APP_ID', 'JS_KEY');
Parse.serverURL = 'https://parseapi.back4app.com/';
这是我的测试部分:
describe('StateComponent', () => {
let component: StateComponent;
let fixture: ComponentFixture<StateComponent>;
let service: TrayStateService;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [StateComponent],
providers: [
TrayStateService,
{ provide: NbDialogService, useValue: {} },
{ provide: NbToastrService, useValue: {} },
ChangeDetectorRef,
AuthSessionQuery,
{
provide: 'env',
useValue: environment
}
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
service = TestBed.inject(TrayStateService);
}));
beforeEach(() => {
fixture = TestBed.createComponent(StateComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should verify that immediate function works properly', fakeAsync(() => {
spyOn(service, 'getAll').and.returnValue(of(MockedDataResponseArray));
component.ngOnInit();
tick(10);
}));
这是我得到的错误:
错误:您需要在使用 Parse 之前调用 Parse.initialize。
解决方案
您可以通过直接分配提供间谍。
let backup: any;
beforeEach(() => {
backup = Parse.initialize;
Parse.initialize = jasmine.createSpy();
});
afterEach(() => Parse.initialize = backup);
// ... your normal code
it('in a test', () => {
// ... your normal code
expect(Parse.initialize).toHaveBeenCalledWith('MY_APP_ID', 'JS_KEY');
expect(Parse.serverURL).toEqual('https://parseapi.back4app.com/');
});
推荐阅读
- mongodb - MongoDB联合所有?
- java - 为什么我需要将 IntStream 映射到 Stream
- docker - 创建默认超级用户角色“cassandra”后,Docker 上的 Cassandra 卡住了
- git - 如何在不实际删除的情况下从暂存中删除
- python - 如何在保持一定顺序的同时合并列表和多维列表
- r - 如何选择适合移植日期的入院/出院窗口
- php - 如何从存储中下载文件?
- ios - SKSpriteNode 和 SKTileMapNode 之间的冲突
- c# - Dropbox 使用带有过滤器的“ListFolderAsync”
- android - 如何从翻译中删除保留空间?