Unit testing the assertion

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+1will 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 actualand a desired value expectedto achieve. Here's a more widely used assertion librarychai.js

chai.js assertion library

  1. Installation and Import
npm i chai
import chai from 'chai'
let expect = chai.expect;
  1. 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 same toas 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:

  1. To assert that the object of
  2. It does not have the word assertion assertion function
  3. With assertions assertions word
  4. 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
    equalIs basically a universal assertion words, the vast majority of assertion can be converted to use equalto achieve.

Determining whether the length of the variable such as 3, we can use lengthOf, it can also be used equalto achieve

expect(foo).have.lengthOf(3);
expect(foo.length).equal(3);

For example, to determine the type of data: we can either use aalso can be used equalto achieve

expect(foo).to.be.a('string');
expect(typeof foo).to.equal('string');
  • .deep
    deepMainly used recursive comparison of key-value pairs, rather than comparing the object itself. Usually with equaland propertyuse.
    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 equaland deepwe basically can achieve most of the assertions. More about the assertion
can refer to the official website

Explanation

Currently only describes the expectuse, in fact chai.jsthere assertand shouldkinds of assertions ways.
Which expectis mainly applied to test drive behavior (Behavior Driven Development.BDD). Other kinds of assertions ways, then later used in the presentation.

Guess you like

Origin www.cnblogs.com/yinhaiying/p/11326217.html