gpt4 book ai didi

angular - 我正在尝试使用 jasmine 和 angular 测试来自服务的 api 请求,但没有成功

转载 作者:行者123 更新时间:2023-11-28 21:35:45 25 4
gpt4 key购买 nike

当我尝试将响应与从服务器返回的对象进行比较时,它返回一条错误消息,指出 Expected undefined to equal Object({ data: '2019-11-21T13:00:03.070Z', desconhecido: false, id: 21214, ip: '444.444.44.44', nomeUsuario: 'rodrigo.spinelli', sucesso: true, usuarioId: 1414, usuarioNome: 'rodrigo abreu' }).:

beforeEach(() => {

TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [AcessoService]
});

httpTestingController = TestBed.get(HttpTestingController);
service = TestBed.get(AcessoService);
});

afterEach(() => {
httpTestingController.verify();
});


const profileInfo = {
data: new Date('2019-11-21T10:00:03.07').toISOString(),
desconhecido: false,
id: 21214,
ip: '444.444.44.44',
nomeUsuario: 'rodrigo.spinelli',
sucesso: true,
usuarioId: 1414,
usuarioNome: 'rodrigo abreu'
};

const index = '1';
const itemsPage = '1';
const usuarioId = '';
const nomeUsuario = '';
const dataIni = new Date('2019-11-01T18:25:39.000Z').toISOString();
const dataFim = new Date('2019-11-30T18:25:39.000Z').toISOString();
const ip = '';
const sucesso = '';
const orderBy = '';

let Acessos;


it('can load instance', () => {
expect(service).toBeTruthy();
});

it('getDepartmentMapping() should return data', () => {

service.getAcessos(index, itemsPage, '', '', dataIni, dataFim, '', '', '').subscribe(res => {
Acessos = res.items;
});

expect(Acessos).toEqual(profileInfo);
});

});

最佳答案

这是因为 getAccessos 具有异步行为。它返回一个 Observable 并且你也订阅它。当它为您提供下一个数据时,您会在上下文中而不是在上下文之外得到结果。 Accessos 在订阅的解析函数之外未定义。

像这样尝试:

...
it('getDepartmentMapping() should return data', async () => {
Acessos = await service.getAcessos(index, itemsPage, '', '', dataIni, dataFim, '', '', '').pipe(take(1)).pipe(map((res) => { return res.items; })).toPromise();
expect(Acessos).toEqual(profileInfo);
});
...

关于angular - 我正在尝试使用 jasmine 和 angular 测试来自服务的 api 请求,但没有成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58982043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com