Sinon如何保留承诺?

时间:2017-01-16 20:11:58

标签: javascript testing promise sinon stub

我有控制器,只要创建控制器的新实例,就会调用以下方法

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保证使用新的View()。render()来进行此测试?

2 个答案:

答案 0 :(得分:2)

根据您提供的信息......:

func add(newLocation location:[String:Any]) {
        let momentaryLat = (location["latitude"] as! NSString).doubleValue
        let momentaryLong = (location["longitude"] as! NSString).doubleValue
        let annotation = MKPointAnnotation()
        annotation.title = location["title"] as? String
        annotation.coordinate = CLLocationCoordinate2D(latitude: momentaryLat as CLLocationDegrees, longitude: momentaryLong as CLLocationDegrees)
        mapView.addAnnotation(annotation)
}

答案 1 :(得分:0)

使用Sinon v2.3.1,您可以执行以下操作。

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();
});
相关问题