bundled sinon with BDD style

20 views
Skip to first unread message

Paul Ward

unread,
Jul 1, 2013, 4:50:52 PM7/1/13
to bust...@googlegroups.com
What's the proper way to use the bundled sinon features when using the BDD style? The following fails with 'this' has no method called 'stub'.

describe('foo', function() {
    describe('bar', function() {
        beforeAll(function() {
            this.bazStub = this.stub(Baz,'qux').callsArgWith(2,null,{
                foobar: 1,
                bazqux: 'testResult'
            });
        });

Christian Johansen

unread,
Jul 2, 2013, 1:49:21 AM7/2/13
to bust...@googlegroups.com
Hmm, that's weird. It should've worked. May be a bug with the beforeAll
hook? I'm pretty sure it works everywhere else.

Christian

goofba...@gmail.com

unread,
Jul 3, 2013, 7:30:44 AM7/3/13
to bust...@googlegroups.com
Yeah, we found that beforeAll exposes pretty much none of the normal interface (i.e. console.log( this ) => { } ).
Instead, we just use beforeAll to initialize spec-scoped variables. This means that you need to import your own sinon if you want to stub during beforeAll, which is pretty rubbish in my opinion....

This code works:

var buster = require("buster");
var sinon = require("sinon");

var Baz = { qux: function() { } };

buster.spec.expose();


describe( "foo", function() {

    describe( "bar", function() {

        var beforeAllStub;

        beforeAll( function() {

            beforeAllStub = sinon.stub(Baz,'qux').callsArgWith( 2, null, {

                foobar: 1,
                bazqux: 'testResult'
            } );

        } );

        before( function() {

            var context = this;

            beforeAllStub( "arg1", "arg2", function( err, result ) {

                context.callbackResult = result;

            } );

        } );

        it( "it should have returned the test result", function() {

            expect( this.callbackResult ).toMatch( {

                foobar: 1,
                bazqux: "testResult"
            } );

        } );

    } );

} );
Reply all
Reply to author
Forward
0 new messages