Javascript 茉莉花。托斯洛错误

Javascript 茉莉花。托斯洛错误,javascript,angularjs,jasmine,bootstrap-modal,Javascript,Angularjs,Jasmine,Bootstrap Modal,我有以下功能: 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(isN

我有以下功能:

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");
    });
});
测试失败并返回:请输入一个有效数字


我一直在玩弄语法,几乎所有的事情似乎都没问题。

你应该传递一个将被执行的函数,而不是结果,你可以用匿名函数试试这个

expect(function() {checkforLandP('£asdfsdfp'); } ).toThrow("Please enter a valid number");
另见此答案: