[英]How can I refactor my code to resolve lint error?
我一直在為 switch case 編寫測試用例,並且有 2 個測試用例。 我收到此 lint 錯誤, Update or refactor this function so that its implementation does not duplicate other
. 由於開關案例包含類似的案例,我需要幫助來重構我的代碼..
這是我的2個案例
it('case 1', () => {
data = merge(Run.String_Window, {
0: '3',
});
expect(service.getName('year-1', element, frequency)).toEqual(serve);
});
it('case 2', () => {
data = merge(Run.String_Window, {
0: '5',
});
expect(service.getName('year-2', element, frequency)).toEqual(serve);
});
如果您想對多個值運行相同的測試,那么您可以創建一個數組,其中每個項目都包含您的輸入數據和您的預期數據。 然后,您可以遍歷該數組並為每個數據集執行測試:
const testCases = [
{
desc: 'case 1',
input: { /* ... */ },
expected: { /* ... */ }
}, {
desc: 'case 2',
input: { /* ... */ },
expected: { /* ... */ }
}
// case 3,4,5....
];
testCases.forEach(tc => {
it(tc.desc, () => {
//...
expect(/*...*/).toEqual(tc.expected);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.