- Mastering JavaScript Functional Programming
- Federico Kereki
- 327字
- 2021-07-02 22:41:11
Testing the solution automatically
Running tests by hand is no good; it gets tiresome, boring, and that leads, after time, to not running the tests any longer. Let's do better, and write some automatic tests with Jasmine. Following the instructions over at https://jasmine.github.io/pages/getting_started.html, I set up a standalone runner:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Jasmine Spec Runner v2.6.1</title>
<link rel="shortcut icon" type="image/png"
href="lib/jasmine-2.6.1/jasmine_favicon.png">
<link rel="stylesheet" href="lib/jasmine-2.6.1/jasmine.css">
<script src="lib/jasmine-2.6.1/jasmine.js"></script>
<script src="lib/jasmine-2.6.1/jasmine-html.js"></script>
<script src="lib/jasmine-2.6.1/boot.js"></script>
<script src="src/once.js"></script>
<script src="tests/once.test.1.js"></script>
</head>
<body>
</body>
</html>
The src/once.js file has the once() definition that we just saw, and tests/once.test.js has the actual suite of tests:
describe("once", () => {
beforeEach(() => {
window.myFn = () => {};
spyOn(window, "myFn");
});
it("without 'once', a function always runs", () => {
myFn();
myFn();
myFn();
expect(myFn).toHaveBeenCalledTimes(3);
});
it("with 'once', a function runs one time", () => {
window.onceFn = once(window.myFn);
spyOn(window, "onceFn").and.callThrough();
onceFn();
onceFn();
onceFn();
expect(onceFn).toHaveBeenCalledTimes(3);
expect(myFn).toHaveBeenCalledTimes(1);
});
});
There are several points to note here:
- In order to spy on a function, it must be associated with an object. (Alternatively, you can also directly create a spy using Jasmine's .createSpy() method.) Global functions are associated with the window object, so window.fn is a way of saying that fn is actually global.
- When you spy on a function, Jasmine intercepts your calls and registers that the function was called, with what arguments, and how many times it was called. So, for all we care, window.fn could simply be null, because it will never be executed.
- The first test only checks that if we call the function several times, it gets called that number of times. This is trivial, but if that didn't happen, we'd be doing something really wrong!
- In the second group of tests, we want to see that the once()-ed function (window.onceFn()) gets called, but only once. So, we tell Jasmine to spy on onceFn, but let calls pass through. Any calls to fn() will also get counted. In our case, as expected, despite calling onceFn() three times, fn() gets called only once.
We can see the results in Figure 2.3:

Figure 2.3 - Running automatic tests on our function, with Jasmine
推薦閱讀
- 微服務與事件驅動架構
- Vue.js 2 and Bootstrap 4 Web Development
- PHP 7底層設計與源碼實現
- 信息可視化的藝術:信息可視化在英國
- ArcGIS By Example
- C語言程序設計學習指導與習題解答
- 小學生C++創意編程(視頻教學版)
- 微信小程序入門指南
- Python之光:Python編程入門與實戰
- Raspberry Pi Robotic Projects(Third Edition)
- Instant Apache Camel Messaging System
- 算法訓練營:海量圖解+競賽刷題(入門篇)
- Android項目實戰:博學谷
- Effective Python:編寫高質量Python代碼的90個有效方法(原書第2版)
- Java Web程序員面試筆試寶典