2017-01-16 12 views
0

は、私は、このメソッドをテストしたいコントローラの新しいインスタンスがシノンどのように約束を約束する?

initialize: function() { 
    var self = this; 
    return new View().render().then(function() { 
     bus.broadcast("INITIALIZED"); 
    }); 
} 

を作成されるたびに呼び出され、以下の方法でコントローラを持っている:

it("should initialise controller", (done) => { 
     bus.subscribe("INITIALIZED", (message, payload) => done()); 
     new Controller(); 
    }); 

約束新しいビューを()スタブする方法レンダリングします。 ()とSinon.JSがこのテストを行うには?ご提供いただいた情報に基づいて

答えて

1

...:Sinonのv2.3.1で

it("should initialise controller", (done) => { 
    var renderStub = sinon.stub(View.prototype, 'render'); 
    // for each view.render() call, return resolved promise with `undefined` 
    renderStub.returns(Promise.resolve()); 

    bus.subscribe("INITIALIZED", (message, payload) => done()); 
    new Controller(); 

    //make assertions... 

    //restore stubbed methods to their original definitions 
    renderStub.restore(); 
}); 
0

、次のようにそれを行うことができます。

const sinon = require('sinon'); 
let sandbox; 
beforeEach('create sinon sandbox',() => { 
    sandbox = sinon.sandbox.create(); 
}); 

afterEach('restore the sandbox',() => { 
    sandbox.restore(); 
}); 

it('should initialize controller', (done) => { 
    sandbox.stub(View.prototype, 'render').resolves(); 

    bus.subscribe("INITIALIZED", (message, payload) => done()); 
    new Controller(); 
});