unit-testing - Grails mock :与 Controller 解耦验证

标签 unit-testing validation grails gorm

我有几个grails Controller ,我已经对其进行了一些生成和修改。我正在使用生成的单元测试并使它们通过,但是我认为我正在努力进行。这就是我所拥有的。

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)expect(Attendance.save()).andReturn(testAttendance)),这样我就不必在setUp方法中创建关联对象网络,而该网络对于验证正在操作的域对象是必需的由 Controller 。

我只是看错了吗?看来我应该能够将 Controller 逻辑与验证逻辑解耦,这样我就可以告诉模拟告诉 Controller 验证通过还是失败。提前致谢。

最佳答案

我认为没有办法告诉模拟程序,由 Controller 处理的某个对象的验证是通过还是失败,但是我可能是错的。但是据我了解,您最关心的是关联对象网络的创建对吗?

不知道您的 Controller 是什么样子,我会猜测您正在通过ID(例如位置)在 Controller 中获取所需的域对象,并通过pidm加载Person等。

为了简化所需域对象的创建,可以使用 .save(validate:false)
您的setUp方法可能如下所示:

location = new Location().save(validate: false)
reason = new Reason().save(validate: false)

如果您只需要具有有效ID的对象,这就足够了。
new Person(pidm: 12345).save(validate: false)
new UserAccount(username: "testuser").save(validate: false)

设置某些字段以能够使用类似UserAccount.findByUserName()的查找器。

因此,如果您的 Controller 做了类似的事情
location = Location.get(params["location.id"])
reason = Reason.get(params["reason.id"])
userAccount = UserAccount.findByUserName(params.username)
...
new Attendance(location: location, reason: reason, userAccount: userAccount, ...)

上述行对于您的setUp方法应该是令人满意的。

.save(validate:false)对于仅设置测试中真正需要的值非常有用。我希望我做对了一切,对我有帮助。

关于unit-testing - Grails mock :与 Controller 解耦验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19527622/

相关文章:

c# - 使用 Moq 模拟单元测试中的执行延迟

visual-studio - Visual Studio 单元测试 - Nunit 等多种情况

html - 如何在html5的TextBox中自动在4位数字后插入连字符 "-"?

php - 使用 HTML5 进行客户端表单验证

Grails GORM 自引用 belongsTo 删除与预期相反的方向

unit-testing - Scala 中提供更多信息的断言

java - 创建 nunit 测试而不使用 api 导出它们

spring - 将服务 Autowiring 到验证器中

grails - 每个环境的Grails Controller Action

grails - Grails手动交易和回滚