我正在使用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/