我有几个我生成并稍作修改的 grails 控制器。我正在处理生成的单元测试并让它们通过,但我认为我正在努力做到这一点。这就是我所拥有的。
package edu.liberty.swiper
import grails.test.mixin.*
import org.junit.*
@TestFor(AttendanceController)
@Mock([Attendance, Location, Reason, Person, LocCapMode, GuestContactMode, UserAccount])
class AttendanceControllerTests {
def location
def reason
void setUp() {
def capMode = new LocCapMode(description: "loc cap mode", username: "testuser").save(failOnError: true)
def guestMode = new GuestContactMode(description: "Guest Contact Mode", username: "testuser").save(failOnError: true)
location = new Location(description: "foo", locCapMode: capMode, username: "testuser", guestContactMode: guestMode).save(failOnError: true)
reason = new Reason(description: "test reason", username: "testuser").save(failOnError: true)
def person = new Person(firstName: "John", lastName: "Smith", lid: "L12345678", mi: "Q", pidm: 12345).save(failOnError: true)
def userAccount = new UserAccount(pidm: 12345, username: "testuser").save(failOnError:true)
}
def populateValidParams(params) {
assert params != null
params.personId = '12345'
params.username = "testuser"
params["location.id"] = location.id
params["reason.id"] = reason.id
params.timeIn = new Date()
}
void testIndex() {
...
}
void testList() {
...
}
void testCreate() {
...
}
void testSave() {
controller.save()
assert model.attendanceInstance != null
assert view == '/attendance/create'
response.reset()
populateValidParams(params)
controller.save()
assert response.redirectedUrl == '/attendance/show/1'
assert controller.flash.message != null
assert Attendance.count() == 1
}
void testEdit() {
...
}
...
我希望的是能够动态模拟域对象,即expect(Attendance.save()).andReturn(null)
or expect(Attendance.save()).andReturn(testAttendance)
,这样我就不必在我的 setUp 方法中创建关联对象的网络,这是验证正在被操作的域对象所必需的控制器。
我只是看错了吗?似乎我应该能够将控制器逻辑与验证逻辑分离。这样我就可以告诉模拟告诉控制器验证通过或失败。提前致谢。