Angular Jasmine模拟提供程序,具有Angluar 9的方法和属性
我试图模仿angularx社交登录npm包。我想要的是默认值应该是创建测试才能通过。在我的测试规范中,我有:Angular Jasmine模拟提供程序,具有Angluar 9的方法和属性,angular,typescript,unit-testing,jasmine,karma-jasmine,Angular,Typescript,Unit Testing,Jasmine,Karma Jasmine,我试图模仿angularx社交登录npm包。我想要的是默认值应该是创建测试才能通过。在我的测试规范中,我有: let component: Component; let fixture: ComponentFixture<Component>; let spy; beforeEach(async(() => { spy = jasmine.createSpyObj('SocialAuthService', ['signIn', 'signOut'],
let component: Component;
let fixture: ComponentFixture<Component>;
let spy;
beforeEach(async(() => {
spy = jasmine.createSpyObj('SocialAuthService', ['signIn', 'signOut'], ['authState']);
TestBed.configureTestingModule({
declarations: [
Component
],
providers: [
{ provide: SocialAuthService, useValue: spy }
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(Component);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
上述结果是可以观察到的。但是,当我在每一行之前的第一行中添加这一行代码时:
spy.authState.and.returnValue(of());
它表示无法读取未定义的属性和。在网上做了一些研究后,我发现很多建议是使用spyOnProperty,但是当我使用类似于
spyOnProperty(spy,'authState','get')代码>获取错误失败::authState未声明为可配置。我真的不知道如何处理这个问题。如果您能提供任何帮助,我将不胜感激。我认为您使用的是jasmine.createSpyObj
。它只需要两个参数,而不是三个
第一个参数是spy的字符串名称,第二个参数是要模拟的公共方法数组。看到您有this.socialAuthService.getAuthState()
,您需要在第二个参数中添加getAuthState
试试这个:
let component: Component;
let fixture: ComponentFixture<Component>;
let spy;
beforeEach(async(() => {
// add all other public properties required into the second argument inside of the array ** in this case remove it since you don't need it
spy = jasmine.createSpyObj('SocialAuthService', []);
TestBed.configureTestingModule({
declarations: [
Component
],
providers: [
{ provide: SocialAuthService, useValue: spy }
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent);
component = fixture.componentInstance;
// I am not sure when you require the value but let's assume you need it in the ngOnInit
// so we have to put it here
// spy.getAuthState.and.returnValue(of(null)); // now we are mocking the return value of getAuthState, ** comment out this line, you don't need it
spy.authState = of(null); // ** mock it to what you would like here
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
let组件:组件;
let夹具:组件夹具;
让间谍;
beforeach(异步(()=>{
//将所需的所有其他公共属性添加到数组**内的第二个参数中。在本例中,请删除它,因为您不需要它
spy=jasmine.createSpyObj('SocialAuthService',[]);
TestBed.configureTestingModule({
声明:[
组成部分
],
供应商:[
{提供:SocialAuthService,useValue:spy}
]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(HeaderComponent);
组件=fixture.componentInstance;
//我不确定您何时需要该值,但假设您在ngOnInit中需要它
//所以我们必须把它放在这里
//spy.getAuthState.and.returnValue(of(null));//现在我们模拟getAuthState的返回值,**注释掉这行,您不需要它
spy.authState=of(null);//**根据您的需要在此处对其进行模拟
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
很抱歉我写错了,我的意思是我需要测试这个。SocialAuthService.authState,它是一个属性而不是一个方法。啊,我看到yep spy.authState=of(null)工作得很好。
let component: Component;
let fixture: ComponentFixture<Component>;
let spy;
beforeEach(async(() => {
// add all other public properties required into the second argument inside of the array ** in this case remove it since you don't need it
spy = jasmine.createSpyObj('SocialAuthService', []);
TestBed.configureTestingModule({
declarations: [
Component
],
providers: [
{ provide: SocialAuthService, useValue: spy }
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent);
component = fixture.componentInstance;
// I am not sure when you require the value but let's assume you need it in the ngOnInit
// so we have to put it here
// spy.getAuthState.and.returnValue(of(null)); // now we are mocking the return value of getAuthState, ** comment out this line, you don't need it
spy.authState = of(null); // ** mock it to what you would like here
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});