Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Angular 6 Jasmine测试中正确配置模拟身份验证服务?_Angular_Testing_Jasmine - Fatal编程技术网

如何在Angular 6 Jasmine测试中正确配置模拟身份验证服务?

如何在Angular 6 Jasmine测试中正确配置模拟身份验证服务?,angular,testing,jasmine,Angular,Testing,Jasmine,我试图在Angular 6 Jasmine组件测试中创建一个模拟AuthService。我似乎无法将其配置为“登录”并正确使用MockAuthService 我缺少了什么样的配置/我有错吗?如何将服务正确地注入到测试中 以下是我收到的错误: TypeError: Cannot read property 'userContext' of null 以及在my.ts文件中发生的位置: this.authService.getCurrentUsers(this.authService.userSe

我试图在Angular 6 Jasmine组件测试中创建一个模拟AuthService。我似乎无法将其配置为“登录”并正确使用MockAuthService

我缺少了什么样的配置/我有错吗?如何将服务正确地注入到测试中

以下是我收到的错误:

TypeError: Cannot read property 'userContext' of null
以及在my.ts文件中发生的位置:

this.authService.getCurrentUsers(this.authService.userSession.userContext.id, this.authService.userSession.authToken)
      .subscribe(data => { ...
这是我的整个测试文件:

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();
  });
});
从'@angular/forms'导入{FormsModule};
从“@angular/router/testing”导入{RouterTestingModule};
从'@angular/common/http'导入{HttpClientModule};
从“./device.component”导入{DeviceComponent};
从“../navigation/navigation.component”导入{NavigationComponent};
从“../sensor page chart/sensor page chart.component”导入{SensorPageChartComponent};
从“../../services/auth.service”导入{AuthService}
描述('DeviceComponent',()=>{
let组件:设备组件;
let夹具:组件夹具;
类MockAuthService{
isLoggedIn=真;
用户会话={
authToken:“27只海龟”,
用户上下文:{
管理员:是的,
公司ID:未定义,
电子邮件:“turtle@place.io",
id:1,
登录:“turtle@place.io",
名称:“海龟用户”,
角色ID:未定义,
状态:“活动”
},
当前应用:{
经典名称:“Terrapinarium”,
描述:“Terrapinarium Desc”,
电子邮件:“turtle@place.io",
启用:对,
名字:“海龟”,
id:1,
姓氏:“用户”,
名称:“Terrapinarium”
}
} 
};
let服务:MockAuthService;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[DeviceComponent、NavigationComponent、SensorPageChartComponent],
导入:[FormsModule、RouterTestingModule、HttpClientModule],
提供程序:[{provide:AuthService,useValue:MockAuthService}]
})
.compileComponents();
service=TestBed.get(MockAuthService)
}));
在每个之前(()=>{
//service=TestBed.get(MockAuthService);
警报(服务)
fixture=TestBed.createComponent(设备组件);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该创建',()=>{
//service=TestBed.get(MockAuthService);
expect(component.toBeTruthy();
});
});

谢谢大家!

e使用
useValue
创建一个模拟类并在提供程序中创建。它应该是
useClass

providers: [{ provide: AuthService, useClass: MockAuthService }]
您还需要在Mock Auth服务中添加
getCurrentUsers()
函数,当currentUsers返回一个observable时,您需要将其设为false并返回一个observable

 let mockAuthService = TestBed.get(AuthService) //get your actual auth service
spyOn(mockAuthService, 'getCurrentUsers').and.returnValue(of({// value goes here})
然后,你需要让它返回一个可观察的

 let mockAuthService = TestBed.get(AuthService) //get your actual auth service
spyOn(mockAuthService, 'getCurrentUsers').and.returnValue(of({// value goes here})

我认为您还应该在
this.authService.getCurrentUsers
上得到一个错误,因为您的模拟服务中没有
getCurrentUsers
方法。我很惊讶为什么你没有得到那个错误。或者你只是没有在问题中添加代码?