我正在为 angular (jasmine) 中的循环编写测试用例并获得 Error: Expected $.length = 11 to equal 3.
。 For 循环假设创建数组数组,如 const result
。谁能告诉我在这里错过了什么?
应用程序
loadImages() {
const rawData = this.data.images;
for (const value of rawData) {
for (const image of value.imageData) {
const v = [value.title, value.documentName, image];
this.collections.push(v);
}
}
}
app.spec.ts
it('should run the loop and push into imageCollection', () => {
const rawData = component.data.images = [
{
'title': 'First Document',
'documentName': 'Kad',
'imageData': [
'one.png',
'two.png'
]
},
{
'title': 'Second Document',
'documentName': 'Kad',
'imageData': [
'three.png'
]
},
];
for (const value of rawData) {
for (const image of value.imageData) {
const v = [value.title, value.documentName, image];
component.collections.push(v);
}
}
});
const result = [
['First Document', 'Kad', 'one.png'],
['First Document', 'Kad', 'two.png'],
['Second Document', 'Kad', 'three.png']
];
expect(component.collections).toEqual(result);
答案 0 :(得分:1)
您需要更改测试以调用您的方法并断言它所做的事情是准确的。
像这样:
it('should run the loop and push into imageCollection', () => {
component.data = {}; // set data property to an object
component.data.images = [ // populate the images property of the object
{
'title': 'First Document',
'documentName': 'Kad',
'imageData': [
'one.png',
'two.png'
]
},
{
'title': 'Second Document',
'documentName': 'Kad',
'imageData': [
'three.png'
]
},
];
component.loadImages(); // call loadImages
const result = [
['First Document', 'Kad', 'one.png'],
['First Document', 'Kad', 'two.png'],
['Second Document', 'Kad', 'three.png']
];
// ensure loadImages is doing what it should be doing.
expect(component.collections).toEqual(result);