本文介紹了如何測試一個函數沒有被調用?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我正在測試路由器并且有兩個函數,我需要測試第一個函數是否被調用而第二個函數沒有.有方法 toHaveBeenCalled
但沒有方法來測試函數是否未被調用.我該如何測試?
I'm testing router and have two functions, and I need to test if first function was called and second was not. There is method toHaveBeenCalled
but there is no method to test if function was not called. How can I test that?
我有這樣的代碼:
var args, controller, router;
beforeEach(function() {
controller = {
foo: function(name, id) {
args = [].slice.call(arguments);
},
bar: function(name) {
}
};
spyOn(controller, "foo").and.callThrough();
spyOn(controller, "bar").and.callThrough();
router = new route();
router.match('/foo/bar/{{id}}--{{name}}', controller.foo);
router.match('/foo/baz/{{id}}--{{name}}', controller.bar);
router.exec('/foo/bar/10--hello');
});
it('foo route shuld be called', function() {
expect(controller.foo).toHaveBeenCalled();
});
it('bar route shoud not be called', function() {
// how to test if bar was not called?
});
推薦答案
使用 not
運算符:
expect(controller.bar).not.toHaveBeenCalled();
這篇關于如何測試一個函數沒有被調用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!