在我的测试规范中,我想为两种东西投保。
1. dummy data maching with model and get response.
2. find the `api` called with correct url.
我正在尝试这些,但出错的原因是:
SubsystemServiceService › get susbsytem collection › should call get the subsytem collection
此外,我想更新我的规格文件,以确保我的两个要求。有人能帮帮我吗?
这是我的spec.ts文件:
import { TestBed } from '@angular/core/testing';
import { HttpClient } from '@angular/common/http';
import { cold } from 'jasmine-marbles';
import { ModelSubSystem } from './../models/model.subSystem';
import { SubsystemService } from './subsystem-service';
describe('SubsystemServiceService', () => {
let service: SubsystemService;
let http: HttpClient;
const data1 = {
Id: 0,
Name: 'subsystem1',
IsDeletePossible: true,
CreatedBy: '',
CreatedDate: new Date(),
UpdatedBy: '',
UpdatedDate: new Date(),
UpdatedByName: '',
CreatedByName: ''
} as ModelSubSystem;
const data2 = {
Id: 0,
Name: 'subsystem2',
IsDeletePossible: true,
CreatedBy: '',
CreatedDate: new Date(),
UpdatedBy: '',
UpdatedDate: new Date(),
UpdatedByName: '',
CreatedByName: ''
} as ModelSubSystem;
const subsystems = [data1, data2];
beforeEach(() => {
TestBed.configureTestingModule({
providers: [{ provide: HttpClient, useValue: { get: jest.fn() } }]
});
service = TestBed.get(SubsystemService);
http = TestBed.get(HttpClient);
});
describe('get susbsytem collection', () => {
it('should call get the subsytem collection', () => {
const response = cold('(-a|)', { a: subsystems });
http.get = jest.fn(() => response);
expect(http.get).toHaveBeenCalledWith(`https://ewsanedevaoscmsapi01-as.websites.net/api/SubSystem`);
});
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
发布于 2019-10-30 09:33:13
你的实际行动在哪里?在一个测试中,应该有三件事
准备工作
描述(“获取susbsytem集合”,() => {)
it('should call get the subsytem collection', () => {
const response = cold('(-a|)', { a: subsystems });
http.get = jest.fn(() => response);
执行
因此,您需要执行一些操作,然后检查结果。有点像
service.get();
断言
expect(http.get).toHaveBeenCalledWith(`https://ewsanedevaoscmsapi01-as.websites.net/api/SubSystem`);
});
});
解释
假设您想测试以下服务方法:
getSubsystems() {
// Some code lead
// ...
// One path leads to get
if (some condition)
return http.get('url');
}
为了测试这个场景
it('should call the url when some condition is fulfilled')
您需要: 1.设置条件,以便实现“某些条件”。创建模拟响应和间谍const response = cold('(-a|)', { a: subsystems });
3。创建一个服务实例并调用代码来测试getSubsystems()
4。断言在调用代码之后,已经用预期的参数调用了http.get。
如果不执行3,则测试框架不知道要执行哪个单元(方法),因此不执行任何操作。
https://stackoverflow.com/questions/58622042
复制相似问题