Unit Testing with Sinon.JS

Preface

Which kind of method is the easiest to test? In my opinion, the answer is like this: Which kind of method is the easiest to test
? In my opinion, the answer should be something like this:

case1 function(s) {
    return s.split(' ').join('.');
}

It is a pure functional method, which can be tested as easily as below
:
assert.equal(case1('Sue Zh'), 'Sue.Zh')
Another method easy to test is that can change something global, like: Another method easy to test is that can change something global, like
: The method of the attribute is also like the above method:

case2 function(s) {
    this.set('name', s.split(' ').join(',');
}


Similarly , it can be tested as easily as below:

case2('Sue Zh');
assert.equal(this.name, 'Sue.Zh');

How to test other kind of methods, such as that only invoke some methods and doesn't return anything or that always throws errors in testing environment, etc. If you are faced with these problems, it's a good idea to try Sinon.JS.
So, how to test other kinds of methods, such as methods that only call other methods and return no value? Another example is the method that keeps reporting errors in the test environment? If you're facing such a problem, try Sinon.JS. Let me introduce
Sinon.JS to you in detail by giving some simple cases.


Case3

case3 function(s) {
    this.anotherMethod1(s.length);
    this.anotherMethod2(s.length+1);
}
what aspects should we test on this method?

For this method, what do we care about when unit testing?
1. if the two methods invoked
whether anotherMethod1 and anotherMethod2 are called
2. if the two methods invoked with corrent parameters
whether the correct parameters are passed to the two methods

By Sinon, we can test the two aspects by spy
:

let spy1 = sinon.spy(this, 'anotherMethod1');
let spy2 = sinon.spy(this, 'anotherMethod2');
case3('Sue Zh');
assert.ok(spy1.calledWith(6));
assert.ok(spy2.calledWith(7));
What’s spy?

It is like a spy you assign to spy a method, which would record something during the method's invocation
.

What else can spy do?
  1. called, notCalled
    If the spy was called.
    Whether the spy snooping method was called.
  2. calledOnce, calledTwice, calledThrice, callCount
    How many times the spy was called
    .
  3. firstCall, secondCall, thirdCall, lastCall, getCall(n), getCalls()
    Get a specific call or all calls
    to get one or all calls.
  4. calledBefore(anotherSpy), calledAfter(anotherSpy), calledImmediatelyBefore(anotherSpy), calledImmediatelyAfter(anotherSpy)
    the order of spies called
    checks the order of method calls spied by each spy.
  5. calledWith(arg1, ...), alwaysCalledWith(arg1, ...), calledWithExactly(arg1, ...), alwaysCalledWithExactly(arg1, ...), calledWithMatch(arg1, ...), neverCalledWith(arg1, ...)
    Check if the spy was exactly, always or never called with args that given or can match
    .
  6. returned(obj), alwaysReturned(obj)
    Check if the spy returned the provided
    value
  7. threw(null|String|Object), alwaysThrew(null|String|Object),
    Check if the spy threw
    exceptions
  8. withArgs(arg1, ...)

    Filter a spy that only spies method invoked with given parameters.
  9. thisValues
    This
    Note that a spy just can spy a method, it is not able to do anything to change the invocation of a method.
    Please note that a spy can only spy on a method, it cannot change the method invocation itself.

Case4

case4 function(s) {
    this.anotherMethod1(s.length); //threw an Exception
    this.anotherMethod2(s.length);//send a XMLHttpResquest
}

We also want to test the two aspects as above, however, as you can see, anotherMethod1 throws an error in testing environment, and anotherMethod2 sends a request which may have side effects, what we can do is to try sinon.stub:
for the above This method, we want to test the same two aspects, however, you will find that anotherMethod1 throws an exception in the test environment, anotherMethod2 will issue a network request, this network request may be abnormal, we can try sinon.stub:

let spy1 = sinon.stub(this, 'anotherMethod1');
let spy2 = sinon.stub(this, 'anotherMethod2');
case4('Sue Zh');
assert.ok(spy1.calledWith(6));
assert.ok(spy2.calledWith(6));
What’s stub?

It is a special kind of spy, but can do more than a pure spy. It creates another method with pre-programmed behavior to replace case4.
Stub is a special kind of spy, but is very intrusive. It creates another method in place of case4 that can give the other method some predefined behavior.

Difference between spy and stub?

A pure spy just spy a method and won't block it but a stuck is a kind of spy that will block a method after it accepts parameters
. A spy that prevents the original method from executing.

What else can stub do?
  1. onCall(n), onFirstCall(), onSecondCall(), onThirdCall()
    When called at some
    time
  2. returns(obj), returnsArg(index), returnsThis()
    Makes the stub return the provided value. Makes the stub return
    a certain value/the incoming parameter of a certain call/this at the time of the call
  3. restore()

    Recovers the method or obj changed by stub
  4. value()
    Assigns a new value to a variable
    to a property of an object
  5. set(setterFn) get(getterFn)
    Defines a new setter or getter for the stub
  6. callThrough()
  7. throws(null|String|Object)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325641244&siteId=291194637