grails - Grails Spock GORM返回一个空列表

标签 grails groovy gorm spock

我正在使用grails GORM构建单元测试。当我调用list()方法接收数据时,列表返回空。
这是 Controller 代码:
println“entity =” + Entity.get(1)

    println "list = "+NotificationProfile.list().size()

    params.max = Math.min((params.max as Integer) ?: 10, 100)
    User user = User.get(springSecurityService.principal.id)

    println "user = "+user

NotificationProfile.List()。size返回0
User.get(springSecurityService.principal.id)返回null

这是规范代码:
@Rollback
@TestFor(NotificationProfileController)
@Mock([NotificationProfile, Entity, User])
class NotificationProfileControllerSpec extends Specification {

def setup() {
}

def cleanup() {
}

void "list by system admin"() {

    when:
    controller.springSecurityService = [principal: [id: 5]]
    controller.list()

    then:
    view == '/notificationProfile/list'

  }
 }

如果我能在列表大小上获得良好的返回并且拥有非null用户,我将很高兴。谢谢。

最佳答案

在进行单元测试时,Grails不会使用数据库连接,因此,对于每个单元测试都没有数据。

为了在运行的单元测试期间填充数据库,您可以在setup()方法中添加数据。

def setup() {
    // make sure you are creating a new user with all the required fields    
    // otherwise GORM will throw validation error
    new User(username: "test", email:"test@test.com").save flush:true, failOnError:true
}

关于grails - Grails Spock GORM返回一个空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39897246/

相关文章:

http - 在grails gsp中访问http请求参数

unit-testing - 如何在grails test-app中查看网络流量? -也许是RESTClient日志记录?

java - 在 Spock 框架中模拟

hibernate - 在集成测试期间,Grails hibernate 事件未触发

hibernate - Grails条件:id不在hasMany列表中

grails - 在 grails 中为当前登录的用户创建

Grails:后台线程插件

android - 在用Kotlin编写的自定义插件中访问gradle Android插件

groovy - Gradle增量任务:将已经生成的代码添加到类路径中

grails - Grails动态查找器能否被应用程序代码破坏?