Unit testing the assertion
As a front-end development, rarely write their own unit tests. Understanding unit testing rarely, a little self-knowledge about unit testing assertions, with some of their own personal understanding, recorded convenient access to the next use.
What is asserted
When you type, the module can accept or process input and output has a very clear definition and understanding, when the output and input program do not match,
you want the program to have a clear return. For example: I know exactly 1+1=2
, I knew I wanted to enter 1+1
will get 2. If not, the program returns an error. At this time we would normally use to assert .
断言
Is designed to verify that the desired output and a consistent tool. In the realization of the content, which is obtained by comparing an actual value actual
and a desired value expected
to achieve. Here's a more widely used assertion librarychai.js
chai.js assertion library
- Installation and Import
npm i chai
import chai from 'chai'
let expect = chai.expect;
- use
let foo = 'bar';
expect(foo).to.equal('bar');
chai use is similar to the form of chained calls. With expect(foo).to.be.a('string');
, for example,
through .
me what you can put this chain is divided into several parts:
expect(foo)
to
be
a('string')
We have gradually come to understand these parts:
expect(foo)
: To assert that you object, which translates to I wish so and so
to
: function words, simply providing a view to improving the readability of the assertion language as a chain, it has asserted itself does not function.
be
: The sameto
as the language calling chain to improve readability.
a(string)
:a(type)
Is a predicate for determining the type of test papers. type is the type of test values, that is, the assertion expectations.
So based on the above analysis, we can see the realization of a need to assert these parts:
- To assert that the object of
- It does not have the word assertion assertion function
- With assertions assertions word
- And a desired result
to assert a desired object and the results are set based on the actual need for input and output. Therefore, we mainly explain the assertion word.
It does not have the word assertion assertion function
- to
- be
- been
- is
- that
- which
- and
- has
- have
- with
- at
- of
- same
- but
- does
- still
these assertions word actually does not make sense, you get rid of it is not affected. such as:
expect(foo).to.equal('bar');
expect(foo).equal('bar');
The above two assertions can achieve the same functionality. That does not make use of these words have little effect.
Then have a word to say about the assertion assertion function.
With assertions assertions word
equal
equal
Is basically a universal assertion words, the vast majority of assertion can be converted to useequal
to achieve.
Determining whether the length of the variable such as 3, we can use lengthOf
, it can also be used equal
to achieve
expect(foo).have.lengthOf(3);
expect(foo.length).equal(3);
For example, to determine the type of data: we can either use a
also can be used equal
to achieve
expect(foo).to.be.a('string');
expect(typeof foo).to.equal('string');
.deep
deep
Mainly used recursive comparison of key-value pairs, rather than comparing the object itself. Usually withequal
andproperty
use.
such as:
expect([1,2]).equal([1,2]) // 断言不通过
The above assertion can not, it is more the type of reference is an object. Next we look at the use ofdeep
expect([1,2]).deep.equal([1,2]) // 断言通过
That is, if an array of objects, etc. When a reference type, to compare the value of which is required deep
.
By using equal
and deep
we basically can achieve most of the assertions. More about the assertion
can refer to the official website
Explanation
Currently only describes the expect
use, in fact chai.js
there assert
and should
kinds of assertions ways.
Which expect
is mainly applied to test drive behavior (Behavior Driven Development.BDD). Other kinds of assertions ways, then later used in the presentation.