【发布时间】:2015-02-22 03:06:24
【问题描述】:
我有一个简单的 grails 控制器:
class AuthorController {
def index(){
def authors = Author.findByFirstName("Albert")
render (view: "author-page", model: ["authors":authors])
}
}
这里,Author 是映射到 SQL 数据库中的表的域类。
我正在尝试像这样为它编写一个单元测试:
import grails.test.mixin.Mock
@Mock(Author)
class AuthorControllerSpec extends Specification {
void "when index is called, authorPage view is rendered"() {
when:
controller.index()
then:
view == "/author-page"
}
}
但是当我运行这个测试时,我不断收到 java.lang.IllegalStateException: Method on class [com.mypackage.Author] was used outside Grails 应用程序。如果在使用模拟 API 或引导 Grails 的测试上下文中正确运行。
谁能告诉我如何正确测试我的操作?我无法模拟 Author.findByFirstName() 方法。
我正在使用 Grails 2.4.2
谢谢。
【问题讨论】:
标签: unit-testing grails grails-domain-class grails-controller