我有一个名为addToShoppingList
的函数,其作用是将用户输入的产品添加到购物清单中(当前只是一个数组)。我正在使用Jest进行测试,以检查我的功能是否正常运行,但是当前卡住了:
index.js
const fetchData = async (product, numOfProducts) => {
const res = await fetch( ... );
const data = await res.json();
return data;
};
// user input for products looks like 'butter 2'
// selecting Breaskstone's butter (from mockData below)
const addToShoppingList = async (product, numOfProducts = 10, getProducts = fetchData) => {
const shoppingList = [];
const item = product.slice(0 ,-2); // i.g. 'butter'
const i = +product.split(' ').pop() - 1; // i.g. '2' which becomes 1, the index number in array
const data = await getProducts(item, numOfProducts);
// i, which is the index, is used to grab specific item from list of products
let products = data.items[i].productInfo.name;
let manufacturers = data.items[i].productInfo.manufacturer;
const chosenItem = `${products} from ${manufacturer}`
shoppingList.push(chosenItem);
return shoppingList;
};
这不是很好的代码,但是我正在尝试学习如何为上述功能编写测试-我目前正在使用Jest并将其记录下来,但是一直收到错误消息(我已经正在谷歌搜索并尝试修复)。很想从单元测试专家那里获得关于我做错了什么的提示:
index.test.js
const mockData = {
items: [
{
productInfo: {
name: 'Butter',
manufacturer: 'Land O Lakes'
},
},
{
productInfo: {
name: 'Butter',
manufacturer: 'Breakstone'
},
},
{
productInfo: {
name: 'Butter',
manufacturer: 'Kerrygold',
},
}
]
};
describe('addToShoppingList', () => {
const mockProducts = jest.fn();
mockGetBooks.mockReturnValue(mockData);
it('should call fetch with correct arguments', async () => {
await addToShoppingList('product 1', 'numOfResults', mockProducts);
expect(mockProducts).toBeCalledWith('product 1', 'numOfResults');
});
});
我想使用上面创建的addToShoppingList
测试我的mockData
函数是否正常工作。
答案 0 :(得分:0)
这是单元测试解决方案:
index.js
:
const fetchData = async (product, numOfProducts) => {
const res = await fetch('https://github.com/mrdulin');
const data = await res.json();
return data;
};
// user input for products looks like 'butter 2'
// selecting Breaskstone's butter (from mockData below)
export const addToShoppingList = async (product, numOfProducts = 10, getProducts = fetchData) => {
const shoppingList = [];
const item = product.slice(0, -2); // i.g. 'butter'
const i = +product.split(' ').pop() - 1; // i.g. '2' which becomes 1, the index number in array
const data = await getProducts(item, numOfProducts);
// i, which is the index, is used to grab specific item from list of products
let products = data.items[i].productInfo.name;
let manufacturers = data.items[i].productInfo.manufacturer;
const chosenItem = `${products} from ${manufacturers}`;
shoppingList.push(chosenItem);
return shoppingList;
};
index.spec.js
:
import { addToShoppingList } from '.';
const mockData = {
items: [
{
productInfo: {
name: 'Butter',
manufacturer: 'Land O Lakes'
}
},
{
productInfo: {
name: 'Butter',
manufacturer: 'Breakstone'
}
},
{
productInfo: {
name: 'Butter',
manufacturer: 'Kerrygold'
}
}
]
};
describe('addToShoppingList', () => {
it('should call fetch with correct arguments', async () => {
const mockProducts = jest.fn().mockReturnValue(mockData);
const actualValue = await addToShoppingList('product 1', 100, mockProducts);
expect(actualValue).toEqual(['Butter from Land O Lakes']);
expect(mockProducts).toBeCalledWith('product', 100);
});
});
单元测试结果:
PASS src/stackoverflow/58807874/index.spec.ts (8.62s)
addToShoppingList
✓ should call fetch with correct arguments (10ms)
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 70 | 40 | 33.33 | 78.57 | |
index.ts | 70 | 40 | 33.33 | 78.57 | 2,3,4 |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 10.145s
源代码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/58807874