【问题标题】:how to dry up these tests with jasmine.js如何用 jasmine.js 干掉这些测试
【发布时间】:2013-05-17 18:07:06
【问题描述】:

我有很多类似以下的测试:

it "Should call togglePadding if df-padding is checked", ->
        spyOn(App.columnsSetupBuildingBlockController.content, 'togglePadding')
        App.view.set("paddingChecked", null)

        Em.run ->
            App.view.set("paddingChecked", true)

        expect(App.columnsSetupBuildingBlockController.content.togglePadding).toHaveBeenCalledWith(true)

    it "Should call togglePadding if df-padding is unchecked", ->
        spyOn(App.columnsSetupBuildingBlockController.content, 'togglePadding')
        App.view.set("paddingChecked", true)

        Em.run ->
            App.view.set("paddingChecked", null)

        expect(App.columnsSetupBuildingBlockController.content.togglePadding).toHaveBeenCalledWith(null)

每个测试中只有几个不同的值是不同的。我如何编写一个共享函数来干燥重复的位并使其看起来更干净?

我也有相同的测试来测试边距、边框等

请帮忙。

谢谢 瑞克

【问题讨论】:

    标签: javascript unit-testing coffeescript jasmine


    【解决方案1】:

    我很确定我在你的问题中遗漏了一些东西......但这就是我理解你想要做的事情

    setup = (mode = null)->
        value1 = mode
        value2 = if not mode then true else null
        spyOn(App.columnsSetupBuildingBlockController.content, 'togglePadding')
        App.view.set("paddingChecked", value1)
    
        Em.run ->
            App.view.set("paddingChecked", value2)
    
        expect(App.columnsSetupBuildingBlockController.content.togglePadding).toHaveBeenCalledWith(value2)
    
    it "Should call togglePadding if df-padding is checked", ->
        setup null
    
    it "Should call togglePadding if df-padding is unchecked", ->
        setup true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多