【问题标题】:How to write spock tests for a service in grails?如何为 grails 中的服务编写 spock 测试?
【发布时间】:2014-06-19 17:13:34
【问题描述】:

我有一个 grails 应用程序,在 grails-app/services/com/myapp/ 中有一个名为 MySampleservice.groovy 的服务

服务有一个方法:

boolean spockTest() {
    return false;
}

我已将其添加到我的BuildConfig.groovy

    test(":spock:0.7") {
        exclude "spock-grails-support"
    }

问题

  • 我应该如何/在哪里为此服务编写 spock 测试?
  • 我将如何运行它?

【问题讨论】:

  • here开始。你应该可以很容易地写一个。

标签: grails spock


【解决方案1】:

一个简单的服务:

// grails-app/services/com/myapp/MySampleService.groovy
package com.myapp

class MySampleService {

    def someMethod(String arg) {
        arg?.toUpperCase()
    }
}

该服务的 Spock 规范:

// test/unit/com/myapp/MySampleServiceSpec.groovy
package com.myapp

import grails.test.mixin.TestFor
import spock.lang.Specification

@TestFor(MySampleService)
class MySampleServiceSpec extends Specification {

    void "test someMethod"() {
        expect:
        service.someMethod(null) == null
        service.someMethod('Thin Lizzy') == 'THIN LIZZY'
    }
}

您可以使用grails test-app unit: 运行测试。

您没有提及您使用的是什么版本的 Grails。使用 2.4.0,您无需在 BuildConfig.groovy 中提及 Spock 即可使其正常工作。

【讨论】:

  • 你的评论说grails test-app unit。我说grails test-app unit:。您是在评论中留下冒号还是您实际运行的冒号?没有冒号,我不希望它起作用。
  • 您的评论在我添加时消失了。这里有一条评论说该命令不起作用,它引用grails test-app unit 作为输入的命令。
  • 我在 grails 2.2.4 上。根据您的回答,我的测试仍未执行。尝试了以下grails test-app unit:grails test-app unit:spockgrails test-app :spock,但我收到消息Tests Passed,但报告显示No tests executed。我什至故意让测试失败,但它说Tests Passed,因为它们从未运行过......
  • 对不起,我有很多 cmets,然后我删除了它们以添加一条评论来解释我尝试过的所有内容。感谢您的帮助
  • 我刚刚创建了一个 2.2.4 应用程序,并将这些类直接从浏览器粘贴到项目中,并且测试工作正常。请参阅github.com/jeffbrown/birdy,其中包括服务的 Spock 规范和 JUnt 测试。如果您运行grails test-app unit:,则测试运行并通过。如果您更改测试中的断言或更改服务代码,测试将失败。
【解决方案2】:

您的服务方法中没有做任何事情,您可以将其测试为

@TestFor(MySampleService)
class MySampleServiceSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "test something"() {
        when:
        def result = service.spockTest()

        then:
        assert result == false
    }
}

服务一般用于数据库交互,比如保存数据,举个简单的例子,下面是我的服务方法

Comment spockTest(String comment) {
    Comment commentInstance = new Comment(comment: comment).save()
    return commentInstance
}

那我一般把这个测试为

@TestFor(MySampleService)
@Mock([Comment])
class MySampleServiceSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "test something"() {
        given:
        Integer originalCommentCount = Comment.count()

        when:
        def result = service.spockTest('My Comment')

        then:
        assert originalCommentCount + 1 == Comment.count()
        assert result.comment == "My Comment"
    }
}

Comment 是我的域类。

我以grails test-app -unit MySampleServiceSpec 运行此测试

【讨论】:

  • 你真的不应该在 Spock Spec 中使用这样的断言。在then: 块而不是assert result.comment == "My Comment" 中,正常的做法就是result.comment == "My Comment"
猜你喜欢
  • 2012-03-29
  • 2013-09-09
  • 1970-01-01
  • 2015-03-03
  • 2015-01-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
相关资源
最近更新 更多