[英]How to properly configure a Mock Auth Service in Angular 6 Jasmine test?
I am trying to create a mock AuthService in my Angular 6 Jasmine component test.我正在尝试在我的 Angular 6 Jasmine 组件测试中创建一个模拟 AuthService。 I can't seem to quite get it configured to "sign in" and use my MockAuthService correctly.
我似乎无法完全将其配置为“登录”并正确使用我的 MockAuthService。
What sort of configurations am I missing/do I have wrong?我缺少什么类型的配置/我有错吗? How can I properly inject my service into my test?
如何正确地将我的服务注入到我的测试中?
Here is the error I'm receiving:这是我收到的错误:
TypeError: Cannot read property 'userContext' of null
And the place in my .ts file where it is occurring:以及它发生在我的 .ts 文件中的位置:
this.authService.getCurrentUsers(this.authService.userSession.userContext.id, this.authService.userSession.authToken)
.subscribe(data => { ...
Here is my entire test file:这是我的整个测试文件:
import { FormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientModule } from '@angular/common/http';
import { DeviceComponent } from './device.component';
import { NavigationComponent } from '../navigation/navigation.component';
import { SensorPageChartComponent } from '../sensor-page-chart/sensor-page-chart.component';
import { AuthService } from '../../services/auth.service'
describe('DeviceComponent', () => {
let component: DeviceComponent;
let fixture: ComponentFixture<DeviceComponent>;
class MockAuthService {
isLoggedIn = true;
userSession = {
authToken: "27turtles",
userContext: {
admin: true,
companyHid: undefined,
email: "turtle@place.io",
id: 1,
login: "turtle@place.io",
name: "Turtle User",
roleHids: undefined,
status: "ACTIVE"
},
currentAplication: {
canonicalName: "Terrapinarium",
description: "Terrapinarium Desc",
email: "turtle@place.io",
enabled: true,
firstName: "Turtle",
id: 1,
lastName: "User",
name: "Terrapinarium"
}
}
};
let service: MockAuthService;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [DeviceComponent, NavigationComponent, SensorPageChartComponent],
imports: [FormsModule, RouterTestingModule, HttpClientModule],
providers: [{ provide: AuthService, useValue: MockAuthService }]
})
.compileComponents();
service = TestBed.get(MockAuthService)
}));
beforeEach(() => {
// service = TestBed.get(MockAuthService);
alert(service)
fixture = TestBed.createComponent(DeviceComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
// service = TestBed.get(MockAuthService);
expect(component).toBeTruthy();
});
});
Thank you!谢谢!
Your'e creating a Mock class and in providers your'e using useValue
.你创建了一个 Mock 类,并在提供者中使用
useValue
。 It should be useClass
instead它应该是
useClass
代替
providers: [{ provide: AuthService, useClass: MockAuthService }]
You would also need to add getCurrentUsers()
function in Mock Auth service and as currentUsers returns an observable you'll need to make it fake return an observable您还需要在 Mock Auth 服务中添加
getCurrentUsers()
函数,并且由于 currentUsers 返回一个 observable,您需要使其伪造返回一个 observable
let mockAuthService = TestBed.get(AuthService) //get your actual auth service
then in you need to make it return an observable然后你需要让它返回一个可观察的
spyOn(mockAuthService, 'getCurrentUsers').and.returnValue(of({// value goes here})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.