茉莉花.toThrow 错误

3

我有如下函数:

function checkforLandP(n){
if(n[0]=== '£' && n[n.length-1] === 'p'){
    // remove the p 
    n =n.slice(0, -1);
    // remove the £
    n = n.substr(1);

    // take the value  and multiply by 100
    n =Math.round(n*100);
    if(isNaN(n)){
        window.alert('Please enter a valid number');
        throw ('Please enter a valid number');
    }
}
return n;
};

并且以下测试:

describe("check for £ and p", function(){
  it("checks the array for £ and p together", function(){
    expect(checkforLandP('£234p')).toBe(23400);
    expect(checkforLandP(234)).toBe(234);
    expect(checkforLandP('asdfsdf')).toBe('asdfsdf');
    expect(checkforLandP('£asdfsdfp')).toThrow("Please enter a valid number");
    });
});

测试失败并返回:请输入有效的数字

我已经尝试了语法,几乎所有东西都没问题。

1个回答

8
你应该传递一个将被执行而不是结果的函数,你可以尝试使用匿名函数来实现。
expect(function() {checkforLandP('£asdfsdfp'); } ).toThrow("Please enter a valid number");

另请参阅此答案:

https://dev59.com/cW855IYBdhLWcg3wynmC#4144803


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接