测试套餐 - testing suites
the 4 major classifications of testing software are:
- Test Runners => karma ava
- Testing Frameworks => jasmine mocha
- Assertion Libraries => chai
- Testing Plugins => sinon
Karma
Karma is a type of test runner which creates a fake server, and then spins up tests in various browsers using data derived from that fake server. Karma is only a test runner, and requires a testing framework such as Mocha to plug into it in order to actually run tests.
Test Runners work on the highest level of abstraction out of all testing software.
Mocha & Chai
We can distinguish between framework (Mocha) methods and assertion library (Chai) methods by looking at the contents of the it block. Methods outside the it block are generally derived from the testing framework. Everything within the it block is code coming from the assertion library. beforeEach, describe, context, it, are all methods extending from Mocha. expect, equal, and exist, are all methods extending from Chai.
|
|
Chai: Assertion Library
Jasmine for example, has it’s own assertion library builtin. Mocha is just structured in such a way where it does need an external assertion library. This makes Mocha more difficult to setup initially, but offers much greater flexibility than frameworks which use a builtin assertion library such as Jasmine.
Sinon: Testing Plugin
Sinon is a plugin which hooks into Chai and gives us the ability to perform a more diverse set of tests. Through the Sinon plugin we can create mocks, stubs, and fake servers:
- Fake server
|
|
- Fake XMLHttpRequest12345678910beforeEach(function() {xhr = sinon.useFakeXMLHttpRequest();requests = [];xhr.onCreate = function(req) {requests.push(req);};});// Simulates a network error on the request. The onerror handler will be called and the status will be 0.requests[0].onerror();
Sinon has a bunch of cool features that allow you to really get into the nooks and crannies of your source code and see what is really going on under the hood.
##