【问题标题】:Data-driven test with mocking带模拟的数据驱动测试
【发布时间】:2014-09-17 12:48:56
【问题描述】:

我有一个接收ContactForm 对象和带有收件人的地图的方法。因此,我编写了以下规范,根据表单的查询返回 Map 中的正确收件人:

def "Correct recipients for first inquiry"() {
    setup:
    def form = Mock(ContactForm)
    form.getInquiry() >> "Subject 1"

    expect:
    sut.getRecipients(form, recipientsTestMap) == ["recipient1"]
}

def "Correct recipients for second inquiry"() {
    setup:
    def form = Mock(ContactForm)
    form.getInquiry() >> "Subject 2"

    expect:
    sut.getRecipients(form, recipientsTestMap) == ["recipient2"]
}

// and so on ...

有没有一种数据驱动的方式来做到这一点?不幸的是,现在不传递表单而是传递查询字符串本身不是一个选项,因为这需要大量重构。我只是好奇 Spock 是否可以进行这种数据驱动,尽管每次测试之前都必须更改模拟。

【问题讨论】:

    标签: unit-testing groovy spock data-driven-tests


    【解决方案1】:

    您可以通过以下方式执行此操作(不确定这是否是您要求的):

    @Unroll
    def "Correct recipients for #inquiry inquiry"() {
        setup:
        def form = Mock(ContactForm)
        form.getInquiry() >> inquiry
    
        expect:
        sut.getRecipients(form, recipientsTestMap) == result
    
        where:
        inquiry     | result
        "Subject 1" | ["recipient1"]
        "Subject 2" | ["recipient2"]
    }
    

    【讨论】:

    • 好的,非常感谢!不知道在设置部分使用这个是可能的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多