首页 文章

Spock参数匹配MarkupBuilder闭包

提问于
浏览
0

我正在尝试测试一个注入了RestClient的Groovy类 .

class MyService {
     def restClient

     def put() {
         restClient.put(
              path: "foo",
              contentType: "XML",
              body: { foo { bar(baz) }}
         )
     }
}

这在手动测试中可行 .

我想用Spock测试它:

@TestFor(MyService)
class MyServiceSpec extends Specification {

    RESTClient restClient = Mock()

    def setup() {
        service.restClient = restClient
    }

    def "posts to restClient"() {
         when:
         service.put()

         then:
         1 * service.restClient.put([
              path: "foo",
              contentType: "XML",
              body: { foo { bar(baz) }}
         ])
    }
}

这失败了:Spock不考虑匹配的参数 . 我怎样才能让Spock认识到他们是相同的论点?

(Grails 2.3.11)

更新:我认为问题的核心是 { foo() } == { foo() } 是假的 . How to assert equality of two closures

我看到我可以捕获Spock中的闭包,从闭包构建XML并比较XML对象 . 实现这一目标的简洁易懂的方法是什么?

1 回答

  • 0

    您可以在Spock中使用_(非核心运算符)来匹配“任何东西”

    then:
             1 * service.restClient.put(_)
    

    应该可以正常工作

相关问题