使用 grails 2.4.4(和 2.4.5)我创建了以下微型应用程序。
grails create-app example
cd example
grails create-domain-class Book
然后编辑 Book.groovy 看起来像
package example
class Book {
String title;
static constraints = {
}
}
然后添加了一个基本的Controller
grails> create-controller example.book
| Created file grails-app/controllers/example/BookController.groovy
| Created file grails-app/views/book
| Created file test/unit/example/BookControllerSpec.groovy
并修改控制器以扩展 RestfulController。
package example
import grails.rest.*
class BookController extends RestfulController<Book> {
static responseFormats=['json','xml']
BookController() {
super(Book)
}
}
连接 UrlMappings 以将书籍作为 /api/books 的资源提供。
class UrlMappings {
static mappings = {
"/api/books"(resources: "book")
"/"(view:"/index")
"500"(view:'/error')
}
}
最后但并非最不重要的一点是,在 BootStrap.groovy 中编写了几本书。
import example.*
class BootStrap {
def init = { servletContext ->
new Book(title: "Book 1").save(failOnError:true);
new Book(title: "Book 2").save(failOnError:true);
new Book(title: "Book 3").save(failOnError:true);
}
def destroy = {
}
}
然后grails run-app
一切看起来都很好。example.Book 控制器显示在索引页面中。三本书可以查看为json或xml。
所以现在进行单元测试。
编辑 BookControllerSpec 使其看起来像这样
package example
import grails.test.mixin.TestFor
import grails.test.mixin.Mock
import spock.lang.Specification
@TestFor(BookController)
@Mock(Book)
class BookControllerSpec extends Specification {
def setup() {
new Book(title: "Unit Test Book 1").save(failOnError:true);
new Book(title: "Unit Test Book 2").save(failOnError:true);
new Book(title: "Unit Test Book 3").save(failOnError:true);
}
void "My Setup Worked"() {
given: "My setup ran"
when: "I ask for the count"
then: "I should get back 3"
Book.count() == 3;
}
void "I can access my Controller index method"() {
given: "My setup ran"
when: "I call index"
request.method="GET"
response.format="xml"
controller.index();
then: "I get an XML object back with 3 books"
println response.contentAsString
response.xml.book*.title.size()==3
}
}
第一个测试通过,第二个测试失败。失败测试中 println 的输出是一个空的 xml 书籍列表。
<?xml version="1.0" encoding="UTF-8"?><list />
在调查发生了什么时,我查看了父类(RestfulController)的索引方法。
通过将该方法复制到 BookController 中,单元测试将开始通过。
-- 新版 BookController 复制索引方法 --
package example
import grails.rest.*
class BookController extends RestfulController<Book> {
static responseFormats=['json','xml']
BookController() {
super(Book)
}
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
respond listAllResources(params), model: [("${resourceName}Count".toString()): countResources()]
}
}
那么,为了让 RestfulController 的 index 方法工作而不必将 index 方法复制到 BookController 中,我还需要添加什么到 Spec 中吗?
知道为什么调用listAllResources
直接从 BookController 执行时有效,但从 RestfulController 执行时不返回任何行。
上面的单元测试是 Grails In Action 书中描述的其余单元测试的修改版本,它是为 grails 2.3 编写的。 http://www.manning.com/gsmith2/