How do I use Sinon with Typescript?

不羁岁月 提交于 2019-11-27 23:04:35

问题


If I use sinon with typescript then how do I cast the sinon mock to an instance of my object?

For instance a SinonMock would be returned but my controller under test may require a specific service passed in to its constructor.

var myServiceMock: MyStuff.MyService = <MyStuff.MyService (sinon.mock(MyStuff.MyService));

controllerUnderTest = new MyStuff.MyController(myServiceMock, $log);

Can sinon be used with Typescript?


回答1:


You may need to use an <any> type assertion to make the type wide before you narrow it to your specific type:

var myServiceMock: MyStuff.MyService = 
    <MyStuff.MyService> <any> (sinon.mock(MyStuff.MyService));

Just to clarify one behaviour of sinon - although you pass in MyStuff.MyService, whatever you pass to the mock method is only used to provide better error messages.

If you want the mock to have methods and properties, you need to add them.

If you want automatically created fakes, you can grab the FakeFactory from tsUnit, which creates a fake version with some default values that you can opt to override - in JavaScript this is pretty easy stuff (plus by not using too much mock functionality, you can ensure you are testing behaviour rather than implementation).

Example use of FakeFactory:

var target = tsUnit.FakeFactory.getFake<RealClass>(RealClass);
var result = target.run();
this.areIdentical(undefined, result);



回答2:


Sinon can create a stub based on a constructor quite easily if, instead of mock, you use the createStubInstance method.

An example using mocha, chai, sinon and sinon-chai, could look like this:

import * as sinon from 'sinon';
import * as chai from 'chai';

// ... imports for the classes under test

const expect    = chai.expect;
const sinonChai = require("sinon-chai");

chai.use(sinonChai);

describe('MyController', () => {
    it('uses MyService', () => {

        let myService  = sinon.createStubInstance(MyStuff.MyService),
            controller = new MyStuff.MyController(myService as any, ...);

        // ... perform an action on the controller 
        // that calls myService.aMethodWeAreInterestedIn

        // verify if the method you're interested in has been called if you want to
        expect(myService.aMethodWeAreInterestedIn).to.have.been.called;
    });
});

I've published an article, which you might find useful if you'd like to learn more about the different test doubles and how to use them with Sinon.js.

Hope this helps!

Jan



来源:https://stackoverflow.com/questions/28156057/how-do-i-use-sinon-with-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!