如何单元测试具有关系映射的grails域类? [英] How to unit test grails domain class that has a relational mapping?
问题描述
我的测试结果给出 com.example.book.Book:null
。当我调试测试时,使用MyBook
创建 b
对象作为其名称
。但是因为它有一个静态映射 belongsTo
,所以测试失败。我如何完成这项工作。当我评论Books.groovy中的 belongsTo
映射时,测试通过。那么,我如何测试带映射的域类。我应该实例化一个 Library
对象并向它添加一个 Book
对象吗?但是,这并不能单独测试域类,因为它是为了进行单元测试,是吗?
以下是我的代码。
域名:
// Book.groovy
package com.example .book
class Book {
static constraint = {
name blank:false,size:2..255,unique:true
}
static belongsTo = [lib:Library]
字符串名称
}
//Library.groovy
包com.example.library
类库{
static hasMany = [book:Book,branch:user:User]
static constraints = {
name blank:false
place blank:false
}
字符串名称
字符串位置
}
单元测试: p>
// BookUnitTests.groovy
包com.example.book
导入grails.test。 *
类BookUnitTests扩展GrailsUnitTestCase {
保护无效setUp(){
super.setUp()
mockForConstraintsTests(Book)
}
protected void tearDown(){
super.tearDown()
}
void testPass(){
def b = new Book(name:MyBook)
assert b.validate()
}
}
测试结果:
失败:testPass(com.example.book.BookUnitTests)
|断言失败:
断言b.validate()
| |
| false
com.example.book.Book:null
at com.example.book.BookUnitTests.testPass(BookUnitTests.groovy:17)
谢谢。
解决方案如果没有图书馆,这本书不能存在。您将不得不创建一个库并将其分配给它。
使用
belongsTo
是否合理取决于您的要求。你是否真的需要保存图书馆,并保存所有书籍?The output of my test gives
com.example.book.Book : null
. When I debug the test,b
object is created with"MyBook"
as itsname
. But since its has a static mappingbelongsTo
, the test fails. How do I make this work. When I comment thebelongsTo
mapping in the Books.groovy, the test passes. So how do I test Domain classes with mappings. Should I instantiate aLibrary
object and add aBook
object to it? But that doesn't make testing the domain class in isolation as it is meant to be in a unit test, does it?Below is my code.
Domains:
//Book.groovy package com.example.book class Book { static constraint = { name blank: false, size: 2..255, unique: true } static belongsTo = [lib: Library] String name } //Library.groovy package com.example.library class Library { static hasMany = [book: Book, branch: user: User] static constraints = { name blank: false place blank: false } String name String place }
Unit tests:
//BookUnitTests.groovy package com.example.book import grails.test.* class BookUnitTests extends GrailsUnitTestCase { protected void setUp() { super.setUp() mockForConstraintsTests(Book) } protected void tearDown() { super.tearDown() } void testPass() { def b = new Book(name: "MyBook") assert b.validate() } }
Test Output:
Failure: testPass(com.example.book.BookUnitTests) | Assertion failed: assert b.validate() | | | false com.example.book.Book : null at com.example.book.BookUnitTests.testPass(BookUnitTests.groovy:17)
Thanks.
解决方案Yes, the way you have this set up the Book cannot exist without a Library. You will have to create a Library and assign the book to it.
Whether using
belongsTo
makes sense depends on your requirements. Do you really need to save the library and have all the books get saved as a result?这篇关于如何单元测试具有关系映射的grails域类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!