2017-02-22 27 views
0

我试图用兴农来测试JS组件,它看起来有点像这样...Sinon.JS - 如何从存根中获取参数?

import Bootbox from "../helpers/bootbox"; 
import Guard from "../helpers/guard"; 
import UrlHelper from "../helpers/url-helper"; 

export default class DeleteButton { 

    /** 
    * Creates an instance of DeleteButton. 
    * 
    * @param {object} element The DOM element to make into a delete button. 
    * 
    * @memberOf DeleteButton 
    */ 
    constructor(element) { 
     Guard.throwIf(element, "element"); 

     this.deleteUri = element.getAttribute("data-delete-uri") || UrlHelper.current.url().split('?')[0];   
     this.title = element.getAttribute("data-title") || `Delete the item?`; 
     this.cancelText = element.getAttribute("data-cancel") || `Cancel`; 
     this.confirmText = element.getAttribute("data-confirm") || `Remove`; 
     this.message = element.getAttribute("data-message") || `Do you want to delete the item? This cannot be undone.`; 
     this.successUri = element.getAttribute("data-success-uri"); 
     this.errorMessage = element.getAttribute("data-error-message") || `Unable to complete operation.`; 
    } 

    /** 
    * Shows failure of deletion. 
    * 
    * @memberOf DeleteButton 
    */ 
    showFailed() { 
     Bootbox.alert({ 
      message: this.errorMessage, 
      size: `small`, 
      backdrop: true 
     }); 
    } 
} 

测试看起来像这样...

it("Can show a fail message", function() { 
    $("body").append(`<a class="js-delete-button" data-id="123" data-delete-uri="/delete/123">Delete</a>`); 
    let objUt = new DeleteButton($(".js-delete-button").get()[0]); 
    let bootboxAlertStub = Sinon.stub(Bootbox, 'alert'); 
    objUt.showFailed(); 
    let args = bootboxAlertStub.args; 
    expect(args.message).to.equal(objUt.errorMessage); 
}); 

但我不能让过去行let bootboxAlertStub = Sinon.stub(Bootbox, 'alert');,因为我从Karma得到一个错误,说'应该包装对象的属性'。我试过用Sinon.test包装器来包装它,并使用this.stub,但是这个错误更加愚蠢。

我已经通过Sinon.JS文档并在网上搜索,但我卡住了。代码工作正常。

我看了这篇文章,这是类似的 - Stubbing a class method with Sinon.js但它不完全相同。

看实际的基础bootbox JavaScript文件,我有效地试图存根,看起来有点像这样(减少)的方法...

exports.alert = function() { 
    // do something 
    }; 

JS文件然后返回在出口结束...

return exports; 

一些Github上张贴寻找它似乎不太可能为标的的调用都是实用的功能,而不是目标函数存根这些特定的呼叫。

我已经改变我的Bootbox包装类如下缓解这种(以可怕的方式)......

export default { 

    /** 
    * Generates an alert box. 
    * 
    * @param {any} options 
    */ 
    alert: function(options) { 
     window.bootbox.alert(options); 
    }, 

    /** 
    * Generates a confirmation dialog. 
    * 
    * @param {any} options 
    */ 
    confirm: function(options) { 
     window.bootbox.confirm(options); 
    } 
} 

这解决了一个问题,并引入了另一个。虽然我现在可以存根Bootbox,存根时,我不能得到的参数....

(从测试)

let args = bootboxAlertStub.args; 

这是令人沮丧的 - 该参数是作为一个复杂的参数,因此一传递'所谓'断言不会削减它。

有什么办法可以得到存根的参数吗?

+0

你试过'bootboxAlertStub.getCall(0).args'吗? –

+0

澄清:传递给'getCall'的参数是调用的“索引”; '0'是第一次打到你的存根,'1'是第二次打电话等等。 –

回答

2

感谢Matt Mokary(如果你要提交一个答案,我给你这一个信贷)

我编辑我的测试如下...

it("Can show a fail message", Sinon.test(function() {   
    $("body").append(`<a class="js-delete-button" data-id="123" data-delete-uri="/delete/123">Delete</a>`); 
    let objUt = new DeleteButton($(".js-delete-button").get()[0]); 
    let bootboxAlertStub = this.stub(Bootbox, 'alert'); 
    objUt.showFailed(); 
    Sinon.assert.called(bootboxAlertStub); 
    let options = bootboxAlertStub.getCall(0).args[0]; 
    expect(options.message).to.equal(objUt.errorMessage); 
})); 

的东西,固定它是,马特建议,使用...

bootboxAlertStub.getCall(0); 

012的小幅调整

获取第一个参数(这是选项对象)。

另外,发现我可以使用console.log来查看当我通过Phantom和Karma运行测试时发生了什么,这既是一个启示,也是一个头衔的时刻。