3

I have the following function:

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;
};

and the following test:

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");
    });
});

The test fails and returns: Please enter a valid number thrown

I have been playing with the syntax and almost everything seems to be ok.

rc_dz
  • 461
  • 4
  • 20

1 Answers1

8

You should pass a function that will be executed not the result, you can try this with anonymous function.

expect(function() {checkforLandP('£asdfsdfp'); } ).toThrow("Please enter a valid number");

See also this answer :

https://stackoverflow.com/a/4144803/2419919

Community
  • 1
  • 1
Moncef Hassein-bey
  • 1,361
  • 9
  • 14