我想知道如何建立一个域类对象的世界以用于我的单元测试。最好的方法是什么?
说这是我的代码ServiceX:
List<Course> getAllCoursesByType(String type) {
List<Course> list = Course.findAllByType(type)
list
}
这是对ServiceX的测试:
import grails.buildtestdata.mixin.Build
import grails.test.mixin.TestFor
import grails.test.mixin.Mock
import spock.lang.Specification
@TestFor(ServiceX)
class ServiceXSpec extends Specification {
void "get all open courses"() {
given:
String type = "open"
when:
def list = service.getAllCoursesByType(type)
then:
list.size() == 4
}
}
如何“预填充”测试数据库(内存),使列表中实际上有4个这样的对象?
最佳答案
为此创建集成测试。查看示例here。
关于unit-testing - 如何在Grails 3中填充域类以进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35600502/