[英]Jasmine Mocking Provider with Method and Properties Angluar 9
I am trying to mock the angularx-social-login npm package.我正在尝试模拟 angularx-social-login npm 包。 All I want is the default should be created test to pass.
我想要的是默认应该创建测试以通过。 In my test spec I have:
在我的测试规范中,我有:
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();
});
With this code I get the error cannot read property subscribe of undefined.使用此代码,我收到错误无法读取未定义的属性订阅。 Which is expected because I haven't setup the subscribe for authState, as in my component I have this:
这是预期的,因为我还没有为 authState 设置订阅,因为在我的组件中我有这个:
this.socialAuthService.authState;
The above returns observable.以上返回可观察的。 However when I then add this line of code in the first before each:
但是,当我在第一个之前添加这行代码时:
spy.authState.and.returnValue(of());
It says cannot read property and of undefined.它说无法读取属性和未定义。 Having done some research online I can see that alot of suggestions is to use spyOnProperty, however when I use something like
spyOnProperty(spy, 'authState', 'get');
在网上做了一些研究后,我可以看到很多建议是使用 spyOnProperty,但是当我使用类似
spyOnProperty(spy, 'authState', 'get');
I get the error Failed: : authState is not declared configurable.我收到错误失败:: authState 未声明为可配置。 I am not really sure how to proceed with this issue any help would be much appreciated.
我不太确定如何处理这个问题,任何帮助将不胜感激。
I think you are using jasmine.createSpyObj
incorrectly.我认为您错误地使用了
jasmine.createSpyObj
。 It only takes 2 arguments, not three.它只需要 2 个参数,而不是三个。
The first argument is a string name for the spy and the second argument is an array of public methods that you would like to mock.第一个参数是间谍的字符串名称,第二个参数是您想要模拟的公共方法数组。 Seeing you have
this.socialAuthService.getAuthState()
, you need to add getAuthState
in this second argument.看到您有
this.socialAuthService.getAuthState()
,您需要在第二个参数中添加getAuthState
。
Try this:尝试这个:
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();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.