angular - Jasmine - “无法读取未定义抛出的属性‘值’”
问题描述
我刚开始使用 Jasmine 为现有的 Angular 应用程序编写单元测试,大约 50% 的时间,我收到以下错误。另外 50% 的时间,所有测试都通过,没有任何问题。
我遇到此错误消息的主要问题是 Jasmine 没有告诉我问题出在哪里
An error was thrown in afterAll
Uncaught TypeError: Cannot read property 'values' of undefined thrown
describe('deal without Angular testing support', () => {
let service: DealService;
let httpMock: HttpTestingController;
beforeEach(async() => {
TestBed.configureTestingModule({
providers: [
DealService
],
imports: [HttpClientTestingModule],
schemas: [ NO_ERRORS_SCHEMA ]
});
service = TestBed.get(DealService);
httpMock = TestBed.get(HttpTestingController);
});
it('should use ValueService', () => {
expect(service).toBeTruthy();
});
it('should return the json for deal http request', () => {
service.getDealParameters({name:'sample name',value: "sample value"}).subscribe(data => {
expect(data[0].dealParameterId).toBe(111);
spyOn(service , "getDealParamDatafromSubject").and.callThrough();
expect(service.getDealParamDatafromSubject).toHaveBeenCalled();
});
const req = httpMock.expectOne('https://localhost:44382/DPasdarameterSetup/GealParameters', 'call to api');
expect(req.request.method).toBe('GET');
req.flush([{
dealParameterId: 111,
actionComments: 'lorem',
value: "sample value 1"
},
{
dealParameterId: 222,
actionComments: 'lorem',
value: "sample value 2"
}]);
httpMock.verify();
});
});
service code:
-----------------
export class DealParameterSetupService {
scope: CScope[];
primaryRestriction: CprimaryRestriction[];
paramType: CparameterType[];
secondaryRestriction: CSecondaryRestriction[];
dealparams: CDealParameterSetup;
dealParamData = new BehaviorSubject<any>('');
dealData: Observable<any>;
test = [];
// Store the deserialize dealdata
getDealParamDatafromSubject(dealData: any) {
this.dealParamData.next(dealData);
}
constructor(private http: HttpClient) {
this.dealData = this.dealParamData.asObservable();
}
// getDealParameters to get deal data
getDealParameters(userDetailsObj): Observable<CDealParameterSetup[]> {
return this.http.get<any>(environment.appBaseUrl + 'DPasdarameterSetup/GetDealParameters')
.pipe(
tap(
(data) => {
this.test = [];
data.map((dealData: CDealParameterSetup) => {
dealData.actionPerformedBy = userDetailsObj.userName;
this.test.push(new CDealParameterSetup().deserialize(dealData));
this.getDealParamDatafromSubject(this.test);
return this.test;
})
}
)
);
}
});
我怎么能找到这个错误来自哪里?有人在 Jasmine 中遇到过任何不一致的情况吗?
解决方案
这可能是随机运行测试的情况。在 jasmine 3 中,以随机顺序运行测试显然现在是默认设置。通过将 random 设置为 false 可以解决此问题。
config.set({
client: {
jasmine: {
random: false
}
}
})
推荐阅读
- javascript - 通过纹理指针获取 WebGLTexture
- c# - 将 Dapper 与 SQL Server 一起使用
- javascript - 关闭切换器时引导导航栏滞后
- c# - Swagger 没有在 Web 界面中拾取 ModelMetadataType 属性?
- dataframe - 选择分组数据框中每个组的第 n 个元素
- python - 由于 idna 版本冲突,Travis 中的 Pip 要求安装失败
- postgresql - 带有计算的 select 语句的 PostgreSQL 异常处理
- c++ - 在类中使用成员类模板特化
- python-3.x - 标记文本标签中带有信号“激活链接”的 PyGtk 分割
- javascript - Bot 在加入和离开频道时播放相同的音频