Grails UnitTest
Hi (it is propably stupid question)
how can acquire Domain class from database in test开发者_如何学Go?
class PollServiceTests extends GrailsUnitTestCase {
Integer id = 1
void testSomething() {
Teacher teacher1 = Teacher.get(id)
assert teacher1 != null
}
}
I always get null or
No signature of method: cz.jak.Teacher.get() is applicable for argument types: (java.lang.Integer) values: [1]
thanks a lot Tom
Grails unit tests do not have access to the Grails environment so you have to mock your Domain objects.
void testSomething() {
mockDomain(Teacher)
def teacher = new Teacher(...)
assertNotNull teacher.save(insert:true)
assertNotNull country.countryId
def found = Teacher.get(country.countryId)
}
There is an optional second parameter to mockDomain that takes a List. If you pass in a list, then the get, list, save, update, delete methods will work on those objects in the List (if any).
void testSomething() {
def teachers = []
for (i in 0..9) {
teachers.add(new Teacher(name: "teacher${it}"))
}
mockDomain Teacher, data
Teacher t2 = Teacher.get(2)
assertEquals "teacher2", t2.name
}
精彩评论