Sinon newbie

65 views
Skip to first unread message

Angelo Chen

unread,
Apr 15, 2013, 4:22:53 AM4/15/13
to Sinon.JS
Hi,

A newbie to Sinonjs, need some help. I'm trying to create a fake
object of a real object for testing, here is the code, seems working,
but looks like not the correct Sinonjs way:

var sinon = require('sinon')

// original object
var obj = {}
obj.getSync= function() {
return '123'
}

obj.getAsync = function(cb) {
cb('abc')
}

// Sinon stubs methods for testing
var stub = sinon.stub(obj, 'getSync', function() {
return '321'
})

var stub2 = sinon.stub(obj, 'getAsync', function(cb) {
cb('XYZ')
})

console.log(obj.getSync())

obj.getAsync(function(item) {
console.log(item)
})

some hints? Thanks,

Angelo

Christian Johansen

unread,
Apr 15, 2013, 6:46:19 AM4/15/13
to sin...@googlegroups.com
Hi Angelo,

Here's "the Sinon way":

> var sinon = require('sinon')
>
> // original object
> var obj = {}
> obj.getSync= function() {
> return '123'
> }
>
> obj.getAsync = function(cb) {
> cb('abc')
> }
>
> // Sinon stubs methods for testing
> var stub = sinon.stub(obj, 'getSync', function() {
> return '321'
> })
>
> var stub2 = sinon.stub(obj, 'getAsync', function(cb) {
> cb('XYZ')
> })

var stub = sinon.stub(obj, "getSync").returns("321");
var stub2 = sinon.stub(obj, "getAsync").yields("XYZ");

Christian

Angelo Chen

unread,
Apr 15, 2013, 7:12:39 AM4/15/13
to Sinon.JS
Thanks for the help, is there a way that we can have only one
variable? if the original object has 10 methods, stubbing out 10
methods requires 10 variables.

Christian Johansen

unread,
Apr 15, 2013, 7:17:40 AM4/15/13
to sin...@googlegroups.com

sinon.stub(object); stubs all the methods.

Christian

Angelo Chen

unread,
Apr 15, 2013, 7:45:39 AM4/15/13
to Sinon.JS
Hi,

say,

var sub = sinon.stub(obj)

then how to attach the new methods?

Christian Johansen

unread,
Apr 15, 2013, 8:08:24 AM4/15/13
to sin...@googlegroups.com
Do you mean "how to override default behavior"?

sinon.sub(object);
object.method.returns(42);

This works if object.method was a function prior to the call to
sinon.stub. To add new methods:

object.newMethod = sinon.stub().returns(13);

Christian

Angelo Chen

unread,
Apr 15, 2013, 9:45:00 AM4/15/13
to Sinon.JS
Thanks, if following method accepts a parameter, how to specify it?

object.newMethod = sinon.stub().returns(13);

Angelo Chen

unread,
Apr 15, 2013, 11:15:25 AM4/15/13
to Sinon.JS
A related question:

this is a exported method:

exports.get_key = function(jrec, cb) {
}

and in another js I tried to stub out this method


var service = require('./services/services')


var stub = sinon.stub(services)
stub.get_key = yields("XYZ"); // this should work, but not accepting
a jrec argument

//callsArgWith(index, arg1, arg2, ...)

how to set stub.get_key to have method with these arguments:

function(jrec, cb)

?


On 4月15日, 下午8时08分, Christian Johansen <chrisj...@gmail.com> wrote:

Christian Johansen

unread,
Apr 15, 2013, 1:00:41 PM4/15/13
to sin...@googlegroups.com
It can take an argument. I suggest you read through the docs:
http://sinonjs.org/docs/

Christian
Reply all
Reply to author
Forward
0 new messages