【问题标题】:Spock and Grails - wait for event to occur from service or controllerSpock 和 Grails - 等待服务或控制器发生事件
【发布时间】:2015-07-17 10:39:22
【问题描述】:

我正在运行一个 spock 测试,在该测试中我需要等待来自服务或控制器的异步事件发生,并断言其内容为真。比如下面的伪代码……

def "testA"() {

when:
//start the event
service.startEvent()
//wait for event to complete, and get it's contents
waitFor  { event.complete } 
result << event.eventContents

then:
data == [1,2,3,4]

}

我的用例是我正在对我的应用程序中的电子邮件功能进行功能测试。我需要应用程序发送邮件,等待电子邮件到达,并断言它的内容是真实的。

【问题讨论】:

  • 我尝试使用platform-core plugin,但我无法按照文档进行操作:/
  • 我可能没有抓住重点,但你不能只使用mailinator 之类的东西吗?将电子邮件发送至 yourTestUser123@mailinator.com,前往 mailinator 网站上的yourTestUser123@mailinator.com 收件箱,并对其内容做出一些断言?
  • 这是个好主意!我使用mailgun,所以它应该是完全可能的。虽然我想我正在寻找的是一种在 Spock 内部触发和等待事件的方法。万一我在不久的将来遇到任何我不能简单地开车去的东西,我可以有一个可重用的实现。

标签: events grails asynchronous spock geb


【解决方案1】:

在服务中提供一个方法来返回邮件是否到达还不够吗? Boolean 的简单返回就足够了。该方法应该可以帮助您等待。

如果您想要等待事件,那么也许您可以在 Spock 中提出增强请求,并提出您认为不错的想法?

【讨论】:

  • 好吧,我实际上需要从控制器获取参数才能完成我的断言。但你是对的,它可以更简单。所以我稍微清理了一下。感谢您的意见
【解决方案2】:

这是一种使用 ArrayBlockingQueue 的简单 Java 风格的方法。如果有人想出更好的答案,我会给他们。

TestSpec.groovy

@Shared
def testService = Holders.applicationContext.getBean("testService")

def "testA"() {

    when:
    def result = testService.eventQueue.poll(90, TimeUnit.SECONDS)

    then:
    result == [1,2,3,4]
}

TestService.groovy

ArrayBlockingQueue eventQueue = new ArrayBlockingQueue(1) //limited to 1 message

TestController.groovy

def testMessage() {
    def data = [1,2,3,4] //Switch with params and real life appplication
    testDataService.eventQueue.add(data)
    render 'OK'
}

【讨论】:

    猜你喜欢
    • 2015-01-07
    • 1970-01-01
    • 2015-06-16
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    相关资源
    最近更新 更多