Should.js
global
assertion assert
assertion bool
assertion chaining
Assertion#beAssertion#an→Assertion#beAssertion#of→Assertion#beAssertion#a→Assertion#beAssertion#and→Assertion#beAssertion#been→Assertion#beAssertion#have→Assertion#beAssertion#has→Assertion#beAssertion#with→Assertion#beAssertion#is→Assertion#beAssertion#which→Assertion#beAssertion#the→Assertion#beAssertion#it→Assertion#be
assertion contain
assertion equality
assertion errors
assertion es6
assertion http
assertion matching
assertion numbers
Assertion#InfinityAssertion#NaNAssertion#aboveAssertion#greaterThan→Assertion#aboveAssertion#aboveOrEqualAssertion#greaterThanOrEqual→Assertion#aboveOrEqualAssertion#approximatelyAssertion#belowAssertion#lessThan→Assertion#belowAssertion#belowOrEqualAssertion#lessThanOrEqual→Assertion#belowOrEqualAssertion#within
assertion promises
assertion property
Assertion#emptyAssertion#keysAssertion#key→Assertion#keysAssertion#lengthAssertion#lengthOf→Assertion#lengthAssertion#ownPropertyAssertion#hasOwnProperty→Assertion#ownPropertyAssertion#propertiesAssertion#propertyAssertion#propertyByPathAssertion#propertyWithDescriptorAssertion#sizeAssertion#value
assertion strings
assertion stubs
Assertion#alwaysCalledOnAssertion#alwaysCalledWithAssertion#alwaysCalledWithExactlyAssertion#alwaysCalledWithMatchAssertion#alwaysCalledWithNewAssertion#alwaysThrewAssertion#callCountAssertion#calledAssertion#calledOnAssertion#calledOnceAssertion#calledThriceAssertion#calledTwiceAssertion#calledWithAssertion#calledWithExactlyAssertion#calledWithMatchAssertion#calledWithNewAssertion#neverCalledWithAssertion#neverCalledWithMatchAssertion#threw
assertion types
Assertion#ArrayAssertion#BooleanAssertion#DateAssertion#ErrorAssertion#FunctionAssertion#NumberAssertion#ObjectAssertion#StringAssertion#argumentsAssertion#Arguments→Assertion#argumentsAssertion#classAssertion#Class→Assertion#classAssertion#instanceofAssertion#instanceOf→Assertion#instanceofAssertion#nullAssertion#Null→Assertion#nullAssertion#typeAssertion#undefinedAssertion#Undefined→Assertion#undefined
“global” Members
Assertion.add(name, func)
Way to extend Assertion function. It uses some logic
to define only positive assertions and itself rule with negative assertion.
All actions happen in subcontext and this method take care about negation.
Potentially we can add some more modifiers that does not depends from state of assertion.
Arguments
-
name(String):Name of assertion. It will be used for defining method or getter on Assertion.prototype
-
func(Function):Function that will be called on executing assertion
Example
Assertion.add('asset', function() {
this.params = { operator: 'to be asset' }
this.obj.should.have.property('id').which.is.a.Number()
this.obj.should.have.property('path')
})
Assertion.addChain(name, [onCall])
Add chaining getter to Assertion like .a, .which etc
Arguments
-
name(string):name of getter
-
[onCall](function):optional function to call
Assertion.alias(from, to)
Create alias for some Assertion property
Arguments
-
from(String):Name of to map
-
to(String):Name of alias
Example
Assertion.alias('true', 'True')
PARAM_REGEXP()
RegExp to match *( ";" parameter ) in RFC 7231 sec 3.1.1.1
parameter = token "=" ( token / quoted-string )
token = 1tchar
tchar = "!" / "#" / "$" / "%" / "&" / "'" / ""
/ "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
/ DIGIT / ALPHA
; any VCHAR, except delimiters
quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
qdtext = HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text
obs-text = %x80-FF
quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
PromisedAssertion(obj)
Assertion used to delegate calls of Assertion methods inside of Promise.
It has almost all methods of Assertion.prototype
Arguments
-
obj(Promise)
TYPE_REGEXP()
RegExp to match type in RFC 7231 sec 3.1.1.1
media-type = type "/" subtype
type = token
subtype = token
should$1(obj)
Our function should
Arguments
-
obj(*):Object to assert
Returns
(should.Assertion)
:Returns new Assertion for beginning assertion chain
Example
var should = require('should');
should('abc').be.a.String();
should.config
Object with configuration.
It contains such properties:
checkProtoEqlboolean - Affect if.eqlwill check objects prototypesplusZeroAndMinusZeroEqualboolean - Affect if.eqlwill treat +0 and -0 as equal
Also it can contain options for should-format.
Example
var a = { a: 10 }, b = Object.create(null);
b.a = 10;
a.should.be.eql(b);
//not throws
should.config.checkProtoEql = true;
a.should.be.eql(b);
//throws AssertionError: expected { a: 10 } to equal { a: 10 } (because A and B have different prototypes)
should.extend([propertyName], [proto])
Allow to extend given prototype with should property using given name. This getter will unwrap all standard wrappers like Number, Boolean, String.
Using should(obj) is the equivalent of using obj.should with known issues (like nulls and method calls etc).
To add new assertions, need to use Assertion.add method.
Arguments
-
[propertyName](string):Name of property to add. Default is
'should'. -
[proto](Object):Prototype to extend with. Default is
Object.prototype.
Returns
({ name: string, descriptor: Object, proto: Object })
:Descriptor enough to return all back
Example
var prev = should.extend('must', Object.prototype);
'abc'.must.startWith('a');
var should = should.noConflict(prev);
should.not.exist(Object.prototype.must);
should.noConflict([desc])
Delete previous extension. If desc missing it will remove default extension.
Arguments
-
[desc]({ name: string, descriptor: Object, proto: Object }):Returned from
should.extendobject
Returns
(Function)
:Returns should function
Example
var should = require('should').noConflict();
should(Object.prototype).not.have.property('should');
var prev = should.extend('must', Object.prototype);
'abc'.must.startWith('a');
should.noConflict(prev);
should(Object.prototype).not.have.property('must');
should.use(f)
Simple utility function for a bit more easier should assertion extension
Arguments
-
f(Function):So called plugin function. It should accept 2 arguments:
shouldfunction andAssertionconstructor
Returns
(Function)
:Returns should function
Example
should.use(function(should, Assertion) {
Assertion.add('asset', function() {
this.params = { operator: 'to be asset' };
this.obj.should.have.property('id').which.is.a.Number();
this.obj.should.have.property('path');
})
})
“assertion” Members
Assertion#any
Any modifier - it affect on execution of sequenced assertion to do not check all, but check any of.
Assertion#assert(expr)
Base method for assertions.
Before calling this method need to fill Assertion#params object. This method usually called from other assertion methods.Assertion#params can contain such properties:
operator- required string containing description of this assertionobj- optional replacement for this.obj, it usefull if you prepare more clear object then givenmessage- if this property filled with string any others will be ignored and this one used as assertion messageexpected- any object used when you need to assert relation between given object and expected. Like given == expected (== is a relation)details- additional string with details to generated message
Arguments
-
expr(*):Any expression that will be used as a condition for asserting.
Example
var a = new should.Assertion(42);
a.params = {
operator: 'to be magic number',
}
a.assert(false);
//throws AssertionError: expected 42 to be magic number
Assertion#fail()
Shortcut for Assertion#assert(false).
Example
var a = new should.Assertion(42);
a.params = {
operator: 'to be magic number',
}
a.fail();
//throws AssertionError: expected 42 to be magic number
Assertion#not
Negation modifier. Current assertion chain become negated. Each call invert negation on current assertion.
“assertion assert” Members
should.deepEqual(actual, expected, [message])
Node.js standard assert.deepEqual.
But uses should.js .eql implementation instead of Node.js own deepEqual.
Arguments
-
actual(*) -
expected(*) -
[message](string)
should.doesNotThrow(block, [message])
Node.js standard assert.doesNotThrow.
Arguments
-
block(Function) -
[message](String)
should.equal(actual, expected, [message])
Node.js standard assert.equal.
Arguments
-
actual(*) -
expected(*) -
[message](string)
should.exist
Assert obj exists, with optional message.
Aliases
Arguments
-
obj(*) -
[msg](String)
Example
should.exist(1);
should.exist(new Date());
should.fail(actual, expected, message, operator)
Node.js standard assert.fail.
Arguments
-
actual(*):Actual object
-
expected(*):Expected object
-
message(string):Message for assertion
-
operator(string):Operator text
should.not.exist
Asserts obj does not exist, with optional message.
Aliases
Arguments
-
obj(*) -
[msg](String)
Example
should.not.exist(null);
should.not.exist(void 0);
should.notDeepEqual(actual, expected, [message])
Node.js standard assert.notDeepEqual.
But uses should.js .eql implementation instead of Node.js own deepEqual.
Arguments
-
actual(*) -
expected(*) -
[message](string)
should.notEqual(actual, expected, [message])
Node.js standard assert.notEqual.
Arguments
-
actual(*) -
expected(*) -
[message](string)
should.notStrictEqual(actual, expected, [message])
Node.js standard assert.notStrictEqual.
Arguments
-
actual(*) -
expected(*) -
[message](string)
should.strictEqual(actual, expected, [message])
Node.js standard assert.strictEqual.
Arguments
-
actual(*) -
expected(*) -
[message](string)
should.throws(block, [error], [message])
Node.js standard assert.throws.
Arguments
-
block(Function) -
[error](Function) -
[message](String)
“assertion bool” Members
Assertion#false([message])
Assert given object is exactly false.
Aliases
Arguments
-
[message](string):Optional message
Example
(true).should.not.be.false();
false.should.be.false();
Assertion#ok()
Assert given object is truthy according javascript type conversions.
Example
(true).should.be.ok();
''.should.not.be.ok();
should(null).not.be.ok();
should(void 0).not.be.ok();
(10).should.be.ok();
(0).should.not.be.ok();
“assertion chaining” Members
“assertion contain” Members
Assertion#containDeep(other)
The same like Assertion#containDeepOrdered but all checks on arrays without order.
Arguments
-
other(*):Nested object
Example
[ 1, 2, 3].should.containDeep([2, 1]);
[ 1, 2, [ 1, 2, 3 ]].should.containDeep([ 1, [ 3, 1 ]]);
Assertion#containDeepOrdered(other)
Assert that given object is contain equally structured object on the same depth level.
If given object is an array and other is an array it checks that the eql elements is going in the same sequence in given array (recursive)
If given object is an object it checks that the same keys contain deep equal values (recursive)
On other cases it try to check with .eql
Arguments
-
other(*):Nested object
Example
[ 1, 2, 3].should.containDeepOrdered([1, 2]);
[ 1, 2, [ 1, 2, 3 ]].should.containDeepOrdered([ 1, [ 2, 3 ]]);
({ a: 10, b: { c: 10, d: [1, 2, 3] }}).should.containDeepOrdered({a: 10});
({ a: 10, b: { c: 10, d: [1, 2, 3] }}).should.containDeepOrdered({b: {c: 10}});
({ a: 10, b: { c: 10, d: [1, 2, 3] }}).should.containDeepOrdered({b: {d: [1, 3]}});
Assertion#containEql(other)
Assert that given object contain something that equal to other. It uses should-equal for equality checks.
If given object is array it search that one of elements was equal to other.
If given object is string it checks if other is a substring - expected that other is a string.
If given object is Object it checks that other is a subobject - expected that other is a object.
Arguments
-
other(*):Nested object
Example
[1, 2, 3].should.containEql(1);
[{ a: 1 }, 'a', 10].should.containEql({ a: 1 });
'abc'.should.containEql('b');
'ab1c'.should.containEql(1);
({ a: 10, c: { d: 10 }}).should.containEql({ a: 10 });
({ a: 10, c: { d: 10 }}).should.containEql({ c: { d: 10 }});
({ a: 10, c: { d: 10 }}).should.containEql({ b: 10 });
// throws AssertionError: expected { a: 10, c: { d: 10 } } to contain { b: 10 }
// expected { a: 10, c: { d: 10 } } to have property b
“assertion equality” Members
Assertion#eql(val, [description])
Deep object equality comparison. For full spec see should-equal tests.
Aliases
Arguments
-
val(*):Expected value
-
[description](string):Optional message
Example
(10).should.be.eql(10);
('10').should.not.be.eql(10);
(-0).should.not.be.eql(+0);
NaN.should.be.eql(NaN);
({ a: 10}).should.be.eql({ a: 10 });
[ 'a' ].should.not.be.eql({ '0': 'a' });
Assertion#equal(val, [description])
Exact comparison using ===.
Aliases
Arguments
-
val(*):Expected value
-
[description](string):Optional message
Example
10.should.be.equal(10);
'a'.should.be.exactly('a');
should(null).be.exactly(null);
Assertion#equalOneOf(vals)
Exact comparison using === to be one of supplied objects.
Arguments
-
vals(*):Expected values
Example
'ab'.should.be.equalOneOf('a', 10, 'ab');
'ab'.should.be.equalOneOf(['a', 10, 'ab']);
“assertion errors” Members
Assertion#throw([message], [properties])
Assert given function throws error with such message.
Aliases
Arguments
-
[message](string|RegExp|Function|Object|GeneratorFunction|GeneratorObject):Message to match or properties
-
[properties](Object):Optional properties that will be matched to thrown error
Example
(function(){ throw new Error('fail') }).should.throw();
(function(){ throw new Error('fail') }).should.throw('fail');
(function(){ throw new Error('fail') }).should.throw(/fail/);
(function(){ throw new Error('fail') }).should.throw(Error);
var error = new Error();
error.a = 10;
(function(){ throw error; }).should.throw(Error, { a: 10 });
(function(){ throw error; }).should.throw({ a: 10 });
(function*() {
yield throwError();
}).should.throw();
“assertion es6” Members
Assertion#iterable()
Assert given object supports es6 iterable protocol (just check
that object has property Symbol.iterator, which is a function)
“assertion http” Members
Assertion#contentType(type, [charset])
Check if response have header content-type with given type and charset
Module
npm i should-http --save-devArguments
-
type(string) -
[charset](string)
Assertion#html()
Shortcut for .should.header('content-type', 'text/html')
Module
npm i should-http --save-devExample
res.should.be.html();
Assertion#json()
Shortcut for .should.header('content-type', 'application/json')
Module
npm i should-http --save-devExample
res.should.be.json();
“assertion matching” Members
Assertion#match(other, [description])
Asserts if given object match other object, using some assumptions:
First object matched if they are equal,
If other is a regexp and given object is a string check on matching with regexp
If other is a regexp and given object is an array check if all elements matched regexp
If other is a regexp and given object is an object check values on matching regexp
If other is a function check if this function throws AssertionError on given object or return false - it will be assumed as not matched
If other is an object check if the same keys matched with above rules
All other cases failed.
Usually it is right idea to add pre type assertions, like .String() or .Object() to be sure assertions will do what you are expecting.
Object iteration happen by keys (properties with enumerable: true), thus some objects can cause small pain. Typical example is js
Error - it by default has 2 properties name and message, but they both non-enumerable. In this case make sure you specify checking props (see examples).
Arguments
-
other(*):Object to match
-
[description](string):Optional message
Example
'foobar'.should.match(/^foo/);
'foobar'.should.not.match(/^bar/);
({ a: 'foo', c: 'barfoo' }).should.match(/foo$/);
['a', 'b', 'c'].should.match(/[a-z]/);
(5).should.not.match(function(n) {
return n < 0;
});
(5).should.not.match(function(it) {
it.should.be.an.Array();
});
({ a: 10, b: 'abc', c: { d: 10 }, d: 0 }).should
.match({ a: 10, b: /c$/, c: function(it) {
return it.should.have.property('d', 10);
}});
[10, 'abc', { d: 10 }, 0].should
.match({ '0': 10, '1': /c$/, '2': function(it) {
return it.should.have.property('d', 10);
}});
var myString = 'abc';
myString.should.be.a.String().and.match(/abc/);
myString = {};
myString.should.match(/abc/); //yes this will pass
//better to do
myString.should.be.an.Object().and.not.empty().and.match(/abc/);//fixed
(new Error('boom')).should.match(/abc/);//passed because no keys
(new Error('boom')).should.not.match({ message: /abc/ });//check specified property
Assertion#matchAny(other, [description])
Asserts if any of given object values or array elements match other object, using some assumptions:
First object matched if they are equal,
If other is a regexp - matching with regexp
If other is a function check if this function throws AssertionError on given object or return false - it will be assumed as not matched
All other cases check if this other equal to each element
Aliases
Arguments
-
other(*):Object to match
-
[description](string):Optional message
Example
[ 'a', 'b', 'c'].should.matchAny(/\w+/);
[ 'a', 'b', 'c'].should.matchAny('a');
[ 'a', 'b', 'c'].should.matchAny(function(value) { value.should.be.eql('a') });
{ a: 'a', b: 'b', c: 'c' }.should.matchAny(function(value) { value.should.be.eql('a') });
Assertion#matchEach(other, [description])
Asserts if given object values or array elements all match other object, using some assumptions:
First object matched if they are equal,
If other is a regexp - matching with regexp
If other is a function check if this function throws AssertionError on given object or return false - it will be assumed as not matched
All other cases check if this other equal to each element
Aliases
Arguments
-
other(*):Object to match
-
[description](string):Optional message
Example
[ 'a', 'b', 'c'].should.matchEach(/\w+/);
[ 'a', 'a', 'a'].should.matchEach('a');
[ 'a', 'a', 'a'].should.matchEach(function(value) { value.should.be.eql('a') });
{ a: 'a', b: 'a', c: 'a' }.should.matchEach(function(value) { value.should.be.eql('a') });
“assertion numbers” Members
Assertion#Infinity()
Assert given object is not finite (positive or negative)
Example
(10).should.not.be.Infinity();
NaN.should.not.be.Infinity();
Assertion#above(n, [description])
Assert given number above n.
Aliases
Arguments
-
n(number):Margin number
-
[description](string):Optional message
Example
(10).should.be.above(0);
Assertion#aboveOrEqual(n, [description])
Assert given number above n.
Aliases
Arguments
-
n(number):Margin number
-
[description](string):Optional message
Example
(10).should.be.aboveOrEqual(0);
(10).should.be.aboveOrEqual(10);
Assertion#approximately(value, delta, [description])
Assert given number near some other value within delta
Arguments
-
value(number):Center number
-
delta(number):Radius
-
[description](string):Optional message
Example
(9.99).should.be.approximately(10, 0.1);
Assertion#below(n, [description])
Assert given number below n.
Aliases
Arguments
-
n(number):Margin number
-
[description](string):Optional message
Example
(0).should.be.below(10);
Assertion#belowOrEqual(n, [description])
Assert given number below n.
Aliases
Arguments
-
n(number):Margin number
-
[description](string):Optional message
Example
(0).should.be.belowOrEqual(10);
(0).should.be.belowOrEqual(0);
“assertion promises” Members
Assertion#Promise()
Assert given object is a Promise
Example
promise.should.be.Promise()
(new Promise(function(resolve, reject) { resolve(10); })).should.be.a.Promise()
(10).should.not.be.a.Promise()
Assertion#finally()
Assert given object is promise and wrap it in PromisedAssertion, which has all properties of Assertion.
That means you can chain as with usual Assertion.
Result of assertion is still .thenable and should be handled accordingly.
Aliases
Returns
(PromisedAssertion)
:Like Assertion, but .then this.obj in Assertion
Example
(new Promise(function(resolve, reject) { resolve(10); }))
.should.be.eventually.equal(10);
// test example with mocha it is possible to return promise
it('is async', () => {
return new Promise(resolve => resolve(10))
.should.be.finally.equal(10);
});
Assertion#fulfilled()
Assert given promise will be fulfilled. Result of assertion is still .thenable and should be handled accordingly.
Aliases
Returns
(Promise)
Example
// don't forget to handle async nature
(new Promise(function(resolve, reject) { resolve(10); })).should.be.fulfilled();
// test example with mocha it is possible to return promise
it('is async', () => {
return new Promise(resolve => resolve(10))
.should.be.fulfilled();
});
Assertion#fulfilledWith()
Assert given promise will be fulfilled with some expected value (value compared using .eql).
Result of assertion is still .thenable and should be handled accordingly.
Aliases
Returns
(Promise)
Example
// don't forget to handle async nature
(new Promise(function(resolve, reject) { resolve(10); }))
.should.be.fulfilledWith(10);
// test example with mocha it is possible to return promise
it('is async', () => {
return new Promise((resolve, reject) => resolve(10))
.should.be.fulfilledWith(10);
});
Assertion#rejected()
Assert given promise will be rejected. Result of assertion is still .thenable and should be handled accordingly.
Returns
(Promise)
Example
// don't forget to handle async nature
(new Promise(function(resolve, reject) { resolve(10); }))
.should.not.be.rejected();
// test example with mocha it is possible to return promise
it('is async', () => {
return new Promise((resolve, reject) => reject(new Error('boom')))
.should.be.rejected();
});
Assertion#rejectedWith()
Assert given promise will be rejected with some sort of error. Arguments is the same for Assertion#throw.
Result of assertion is still .thenable and should be handled accordingly.
Returns
(Promise)
Example
function failedPromise() {
return new Promise(function(resolve, reject) {
reject(new Error('boom'))
})
}
failedPromise().should.be.rejectedWith(Error);
failedPromise().should.be.rejectedWith('boom');
failedPromise().should.be.rejectedWith(/boom/);
failedPromise().should.be.rejectedWith(Error, { message: 'boom' });
failedPromise().should.be.rejectedWith({ message: 'boom' });
// test example with mocha it is possible to return promise
it('is async', () => {
return failedPromise().should.be.rejectedWith({ message: 'boom' });
});
“assertion property” Members
Assertion#empty()
Asserts given object is empty. For strings, arrays and arguments it checks .length property, for objects it checks keys.
Example
''.should.be.empty();
[].should.be.empty();
({}).should.be.empty();
Assertion#keys(keys)
Asserts given object has such keys. Compared to properties, keys does not accept Object as a argument.
When calling via .key current object in assertion changed to value of this key
Aliases
Arguments
-
keys(*):Keys to check
Example
({ a: 10 }).should.have.keys('a');
({ a: 10, b: 20 }).should.have.keys('a', 'b');
(new Map([[1, 2]])).should.have.key(1);
json.should.have.only.keys('type', 'version')
Assertion#length(n, [description])
Asserts given object has property length with given value n
Aliases
Arguments
-
n(number):Expected length
-
[description](string):Optional message
Example
[1, 2].should.have.length(2);
Assertion#ownProperty(name, [description])
Asserts given object has own property. On success it change given object to be value of property.
Aliases
Arguments
-
name(string):Name of property
-
[description](string):Optional message
Example
({ a: 10 }).should.have.ownProperty('a');
Assertion#properties(names)
Asserts given object has properties. On this method affect .any modifier, which allow to check not all properties.
Arguments
-
names(...Array|string|Object):Names of property
Example
({ a: 10 }).should.have.properties('a');
({ a: 10, b: 20 }).should.have.properties([ 'a' ]);
({ a: 10, b: 20 }).should.have.properties({ b: 20 });
Assertion#property(name, [val])
Asserts given object has property with optionally value. On success it change given object to be value of property.
Arguments
-
name(string):Name of property
-
[val](*):Optional property value to check
Example
({ a: 10 }).should.have.property('a');
Assertion#propertyByPath(properties)
Asserts given object has nested property in depth by path. On success it change given object to be value of final property.
Arguments
-
properties(...Array|string):Properties path to search
Example
({ a: {b: 10}}).should.have.propertyByPath('a', 'b').eql(10);
Assertion#propertyWithDescriptor(name, desc)
Asserts given object has some descriptor. On success it change given object to be value of property.
Arguments
-
name(string):Name of property
-
desc(Object):Descriptor like used in Object.defineProperty (not required to add all properties)
Example
({ a: 10 }).should.have.propertyWithDescriptor('a', { enumerable: true });
Assertion#size(s)
Asserts given object has such size.
Arguments
-
s(number):Size to check
Example
({ a: 10 }).should.have.size(1);
(new Map([[1, 2]])).should.have.size(1);
“assertion strings” Members
Assertion#endWith(str, [description])
Assert given string ends with prefix
Arguments
-
str(string):Prefix
-
[description](string):Optional message
Example
'abca'.should.endWith('a');
“assertion stubs” Members
Assertion#alwaysCalledOn(obj)
Assert stub was called with given object as this always. So if you call stub several times
all should be with the same object
Module
npm i should-sinon --save-devArguments
-
obj(*):- object that was used as this
Example
var callback = sinon.spy();
var obj = {};
callback.call(obj);
callback.should.be.alwaysCalledOn(obj);
Assertion#alwaysCalledWith(args)
Module
npm i should-sinon --save-devArguments
-
args(*):- arguments that was used for calling
Example
var callback = sinon.spy();
callback(1, 2, 3);
callback.should.be.alwaysCalledWith(1, 2, 3);
Assertion#alwaysCalledWithExactly(args)
Passes if the spy was always called with the provided arguments and no others.
Module
npm i should-sinon --save-devArguments
-
args(*):- arguments that was used for calling
Assertion#alwaysCalledWithMatch(args)
Returns true if spy was always called with matching arguments (and possibly others).
Module
npm i should-sinon --save-devArguments
-
args(*):- arguments that was used for calling
Assertion#alwaysCalledWithNew()
Module
npm i should-sinon --save-devExample
var Class = sinon.spy();
var c1 = new Class();
var c2 = new Class();
Class.should.be.alwaysCalledWithNew;
Assertion#alwaysThrew(ex)
Passes if the spy always threw the given exception. The exception can be a
string denoting its type, or an actual object. If no argument is
provided, the assertion passes if the spy ever threw any exception.
Module
npm i should-sinon --save-devArguments
-
ex(string|Error):- exception to be thrown
Assertion#callCount(count)
Assert stub was called at exact number of times
Module
npm i should-sinon --save-devArguments
-
count(Number):- number of calles
Example
var callback = sinon.spy();
callback.should.have.callCount(0);
callback();
callback.should.have.callCount(1);
callback();
callback.should.have.callCount(2);
Assertion#called()
Assert stub was called at least once
Module
npm i should-sinon --save-devExample
var callback = sinon.spy();
callback();
callback.should.be.called();
Assertion#calledOn(obj)
Assert stub was called with given object as this
Module
npm i should-sinon --save-devArguments
-
obj(*):- object that was used as this
Example
var callback = sinon.spy();
var obj = {};
callback.call(obj);
callback.should.be.calledOn(obj);
Assertion#calledOnce()
Assert stub was called at exactly once
Module
npm i should-sinon --save-devExample
var callback = sinon.spy();
callback();
callback.should.be.calledOnce();
Assertion#calledThrice()
Assert stub was called at exactly thrice
Module
npm i should-sinon --save-devExample
var callback = sinon.spy();
callback();
callback();
callback();
callback.should.be.calledThrice();
Assertion#calledTwice()
Assert stub was called at exactly twice
Module
npm i should-sinon --save-devExample
var callback = sinon.spy();
callback();
callback();
callback.should.be.calledTwice();
Assertion#calledWith(args)
Asserts that stub was called with given arguments
Module
npm i should-sinon --save-devArguments
-
args(*):- arguments that was used for calling
Example
var callback = sinon.spy();
callback(1, 2, 3);
callback.should.be.calledWith(1, 2, 3);
Assertion#calledWithExactly(args)
Returns true if call received provided arguments and no others.
Module
npm i should-sinon --save-devArguments
-
args(*):- arguments that was used for calling
Assertion#calledWithMatch(args)
Returns true if spy was called with matching arguments (and possibly others).
Module
npm i should-sinon --save-devArguments
-
args(*):- arguments that was used for calling
Assertion#calledWithNew()
Asserts that stub was called with new
Module
npm i should-sinon --save-devExample
var Class = sinon.spy();
var c = new Class();
Class.should.be.calledWithNew;
Assertion#neverCalledWith(args)
Returns true if the spy/stub was never called with the provided arguments.
Module
npm i should-sinon --save-devArguments
-
args(*):- arguments that was used for calling
Example
var callback = sinon.spy();
callback(1, 2, 3);
callback.should.be.neverCalledWith(1, 2, 3);
Assertion#neverCalledWithMatch(args)
Returns true if the spy/stub was never called with matching arguments.
Module
npm i should-sinon --save-devArguments
-
args(*):- arguments that was used for calling
Assertion#threw(ex)
Passes if the spy threw the given exception. The exception can be a
string denoting its type, or an actual object. If no argument is
provided, the assertion passes if the spy ever threw any exception.
Module
npm i should-sinon --save-devArguments
-
ex(string|Error):- exception to be thrown
“assertion types” Members
Assertion#class()
Assert given object has some internal [[Class]], via Object.prototype.toString call
Aliases
Assertion#instanceof(constructor, [description])
Assert given object is instance of constructor
Aliases
Arguments
-
constructor(Function):Constructor function
-
[description](string):Optional message
