Arrays 测试排序函数时出现角度测试问题
我有一个排序函数,它接受一个字符串数组,然后使用这个数组按对象的第一个属性对对象数组进行排序 排序功能按预期工作。该组件拥有该函数,并且由于它只需要按照函数不接受参数的特定方式进行排序,因此它简单地使用作为该组件属性的数组,并对作为该组件属性的对象数组进行排序 即: 这就是问题的背景。我有一个角度测试,它测试函数,运行它,然后期望数组与排序数组匹配。似乎发生的是,它要么没有得到分类,要么在预期之前就被棱角分明的/茉莉花弄乱了 我试过: Fixture.detectChanges(); fakeAsync和运行tick()和flushMicrotasks()的内部 即使使用setTimeout 我不知道发生了什么,为什么测试失败了。下面是失败测试的屏幕截图和代码Arrays 测试排序函数时出现角度测试问题,arrays,angular,sorting,testing,jasmine,Arrays,Angular,Sorting,Testing,Jasmine,我有一个排序函数,它接受一个字符串数组,然后使用这个数组按对象的第一个属性对对象数组进行排序 排序功能按预期工作。该组件拥有该函数,并且由于它只需要按照函数不接受参数的特定方式进行排序,因此它简单地使用作为该组件属性的数组,并对作为该组件属性的对象数组进行排序 即: 这就是问题的背景。我有一个角度测试,它测试函数,运行它,然后期望数组与排序数组匹配。似乎发生的是,它要么没有得到分类,要么在预期之前就被棱角分明的/茉莉花弄乱了 我试过: Fixture.detectChanges(); fakeA
describe('the setRingOrder method', () => {
it(
'should set the order of the rings correctly',
waitForAsync(() => {
const fakeRings: Array<RingModel> = [
{ name: 'adopt', color: '' },
{ name: 'trial', color: '' },
{ name: 'hold', color: '' },
{ name: 'asses', color: '' },
];
const ringsInOrder: Array<RingModel> = [
{ name: 'adopt', color: '' },
{ name: 'asses', color: '' },
{ name: 'trial', color: '' },
{ name: 'hold', color: '' },
];
component.rings = fakeRings;
component.setRingOrder();
expect(component.rings).toEqual(ringsInOrder);
})
);
});
编辑:只想说明我在谷歌上搜索了各种帮助,没有任何结果与我的问题完全匹配…您只想测试组件中隔离的
setRingOrder
的功能。您可以创建该组件的实例并指定其中存在的属性,然后可以运行该函数。您知道给定的输入和可以帮助您测试的预期输出
describe('the setRingOrder method', () => {
it(
'should set the order of the rings correctly',
waitForAsync(() => {
const fakeRings: Array<RingModel> = [
{ name: 'adopt', color: '' },
{ name: 'trial', color: '' },
{ name: 'hold', color: '' },
{ name: 'asses', color: '' },
];
const ringsInOrder: Array<RingModel> = [
{ name: 'adopt', color: '' },
{ name: 'asses', color: '' },
{ name: 'trial', color: '' },
{ name: 'hold', color: '' },
];
const component: ComponentType = new ComponentName();
component.rings = fakeRings;
// component.ringsInOrder set this too.
component.setRingOrder();
expect(component.rings).toEqual(ringsInOrder);
})
);
});
description('设置顺序方法',()=>{
它(
“应正确设置环的顺序”,
waitForAsync(()=>{
常量伪造:数组=[
{名称:'采用',颜色:'},
{名称:'试用',颜色:'},
{名称:'保留',颜色:'},
{名称:'asses',颜色:'},
];
常量ringsInOrder:数组=[
{名称:'采用',颜色:'},
{名称:'asses',颜色:'},
{名称:'试用',颜色:'},
{名称:'保留',颜色:'},
];
常量组件:ComponentType=新组件名称();
组件。环=赝品;
//component.ringsInOrder也设置了此选项。
setRingOrder();
expect(component.rings).toEqual(ringsInOrder);
})
);
});
如果您需要更多信息,您可以在post上查看。由于此处的问题,测试wsas的类型更改如下
describe('the setRingOrder method', () => {
it(
'should set the order of the rings correctly',
waitForAsync(() => {
const fakeRings: Array<RingModel> = [
{ name: 'adopt', color: '' },
{ name: 'trial', color: '' },
{ name: 'hold', color: '' },
{ name: 'asses', color: '' },
];
component.rings = fakeRings;
spyOn(component.rings, 'sort');
component.setRingOrder();
expect(component.rings.sort).toHaveBeenCalledTimes(
component.rings.length
);
})
);
description('设置顺序方法',()=>{
它(
“应正确设置环的顺序”,
waitForAsync(()=>{
常量伪造:数组=[
{名称:'采用',颜色:'},
{名称:'试用',颜色:'},
{名称:'保留',颜色:'},
{名称:'asses',颜色:'},
];
组件。环=赝品;
spyOn(component.rings,'sort');
setRingOrder();
预期(组件.环.排序).已被调用的时间(
组件、环、长度
);
})
);
Hi Apoorva.我在测试文件的开头创建了组件,正如您所看到的,我使用component.setRingOrder()调用它;我的问题是,由于测试中的某些原因,它没有正确排序。它在测试中的正确含义是什么?它是否会引发错误或导致不同的o/p?只是为了知道组件中的环是什么?我猜您使用了两个属性ringOrder
和rings
,您需要设置这两个属性以获得正确的结果。Hi Apoorva我在初始post中添加了错误的屏幕截图。当使用如下导出数组创建组件时,会设置RingOrder:string[]=RingOrder;当我们为组件配置测试台时,我们也会这样做。您可以添加组件代码和传递给sort方法的示例数据吗。我想查看这些内容以作进一步评论。您是否能够修复该问题,或者仍然停留在该问题上?
describe('the setRingOrder method', () => {
it(
'should set the order of the rings correctly',
waitForAsync(() => {
const fakeRings: Array<RingModel> = [
{ name: 'adopt', color: '' },
{ name: 'trial', color: '' },
{ name: 'hold', color: '' },
{ name: 'asses', color: '' },
];
const ringsInOrder: Array<RingModel> = [
{ name: 'adopt', color: '' },
{ name: 'asses', color: '' },
{ name: 'trial', color: '' },
{ name: 'hold', color: '' },
];
const component: ComponentType = new ComponentName();
component.rings = fakeRings;
// component.ringsInOrder set this too.
component.setRingOrder();
expect(component.rings).toEqual(ringsInOrder);
})
);
});
describe('the setRingOrder method', () => {
it(
'should set the order of the rings correctly',
waitForAsync(() => {
const fakeRings: Array<RingModel> = [
{ name: 'adopt', color: '' },
{ name: 'trial', color: '' },
{ name: 'hold', color: '' },
{ name: 'asses', color: '' },
];
component.rings = fakeRings;
spyOn(component.rings, 'sort');
component.setRingOrder();
expect(component.rings.sort).toHaveBeenCalledTimes(
component.rings.length
);
})
);