問題描述
我有獲得 3 個參數的函數.我想檢查這個函數沒有拋出錯誤.我做了這樣的事情:
I have function that gets 3 arguments. I want to check that this function not throwing an error. I did something like this:
expect(myFunc).not.toThrow();
問題是 myFunc
需要獲取參數.如何發(fā)送參數?
The problem is myFunc
need to get arguments. How can I send the arguments?
P.S 我試圖傳遞它的參數,但我得到錯誤 myFunc(arg1, arg2, arg3)
不是一個函數.
P.S I tried to pass it argument but I got error that myFunc(arg1, arg2, arg3)
is not a function.
推薦答案
toThrow
匹配器需要將函數作為參數傳遞給 expect
,這樣你就可以簡單地包裝你的函數調用在匿名函數中:
toThrow
matcher requires function to be passed as argument to expect
so you can simply wrap your function call in anonymous function:
expect(function() {
myFunc(arg1, arg2, arg3);
}).not.toThrow();
您也可以使用 綁定創(chuàng)建函數的新版本",在調用時將傳遞提供的參數:
You can also use bind to create new 'version' of your function that when called will be passed provided arguments:
expect(myFunc.bind(null, arg1, arg2, arg3)).not.toThrow();
這篇關于期望不要拋出帶參數的函數 - Jasmine的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!