我已經在Grails 2.x中創建了一個集成測試,測試幾個控制器以模擬Web應用程序中的完整用戶流。grails 2集成測試
首先,我保存了兩個簡單的域對象記錄,在我的測試用例中實例化了兩個控制器實例。這兩個簡單的類是: - 類別(產品的類別)和 - 商店(產品銷售的小型市場) 我寫了兩個測試方法來控制控制器實例(StoreController和CategoryController)並且工作正常,一個類別記錄還創建了一些商店記錄。
然後,我寫了第三種方法來嘗試保存產品記錄。該記錄必須引用一個類別。因此,我從先前的方法中取出類別實例,並嘗試將其傳遞給其他產品參數中的ProductController。
由於未知原因,我只能通過在Web瀏覽器中鍵入數據的Web應用程序實例創建產品記錄,但不能通過測試類代碼創建產品記錄。 我認爲問題是產品和類別之間的關係,但我無法弄清楚我應該如何將參數傳遞給我的測試用例中的ProductController以創建新的產品記錄。
這是我的產品類別:
class Product {
int id
String barCode
String shortDesc
String longDesc
String format
Category category
static belongsTo = [category: Category]
//static hasManySurveyRecord = [surveyRecord: SurveyRecord]
static constraints = {
id editable: false
barCode blank: false, nullable: false, maxSize: 64
shortDesc blank: false, nullable: false, maxSize: 32
longDesc blank: false, nullable: false, maxSize: 128
category blank: false
}
static mapping = {
table 'product_catalog'
version false
columns {
id column: 'id'
barCode column: 'bar_code'
shorDesc column: 'short_desc'
longDesc column: 'long_desc'
format column: 'format'
}
}
String toString() {
return longDesc
}
}
這是我的分類等級:
class Category {
int id
String description
static hasManyProduct = [products: Product]
static constraints = {
id editable: false
description blank: false, nullable: false, maxSize: 64
}
static mapping = {
table 'categories'
version false
columns {
id column: 'id'
description column: 'description'
}
}
String toString() {
return description
}
}
這是我爲這個應用程序集成測試:
static transactional = false
static storeList = []
static storesData = []
static categoryIns
static categoryData = []
static productIns = new Product()
static productData = []
@Before
void setup() {
storesData.add([zipCode: 926260001, address: "first test avenue, first city, CA" , description: "first testing store"])
storesData.add([zipCode: 926260002, address: "second test avenue, second city, CA" , description: "second testing store"])
storesData.add([zipCode: 926260003, address: "third test avenue, third city, CA" , description: "third testing store"])
storesData.add([zipCode: 926260004, address: "fourth test avenue, fourth city, CA" , description: "fourth testing store"])
storesData.add([zipCode: 926260005, address: "fifth test avenue, fifth city, CA" , description: "fifth testing store"])
storesData.add([zipCode: 926260006, address: "sixth test avenue, sixth city, CA" , description: "sixth testing store"])
storesData.add([zipCode: 926260007, address: "seventh test avenue, seventh city, CA", description: "seventh testing store"])
storesData.add([zipCode: 926260008, address: "eighth test avenue, eighth city, CA" , description: "eighth testing store"])
categoryData = [description: "testing category"]
productData = [barCode: "0114B", shorDesc: "The short testing description", longDesc: "The long testing description ....", format: "1 LT"]
}
@Test
void _01__createStores() {
def storeCtl = new StoreController()
(0..7).each { i ->
def model = storeCtl.create()
assert model.storeInstance != null
storeCtl.response.reset()
storeCtl.params.putAll(storesData.get(i))
storeCtl.save()
assert Store.count() == (i+1)
}
storeList.addAll(Store.list())
assert storeList.size() == Store.list().size()
}
@Test
void _02__createCategory() {
// Test if there is a previous store list created
assert storeList.size() == Store.list().size()
def categoryCtl = new CategoryController()
def model = categoryCtl.create()
assert model.categoryInstance != null
categoryCtl.response.reset()
categoryCtl.params.putAll(categoryData)
categoryCtl.save()
assert Category.count() == 1
categoryIns = Category.list()[0]
}
@Test
void _03__createProduct() {
// Test if there is a previous store list created
assert storeList.size() == Store.list().size()
// Test if there is a previous category created
assert categoryIns != null
assert categoryIns.id > 0
def productCtl = new ProductController()
def model = productCtl.create()
assert model.productInstance != null
productCtl.response.reset()
productData.put("category", [id: categoryIns.id])
productData.put("category.id", categoryIns.id)
productCtl.params.putAll(productData)
//productCtl.params.category = Category.get(categoryIns.id)
productCtl.save()
assert Product.count() == 1
}
這是我的ProductController的代碼摘錄:
def save() {
def productInstance = new Product(params)
if (!productInstance.save(flush: true)) {
render(view: "create", model: [productInstance: productInstance])
return
}
flash.message = message(code: 'default.created.message', args: [message(code: 'product.label', default: 'Product'), productInstance.id])
redirect(action: "show", id: productInstance.id)
}
對不起, 在測試方法「void _03__createProduct()」中斷言失敗是「assert Product.count()== 1」。這只是最後一個。 謝謝 – donkanmcleod 2013-03-14 14:24:50
'productCtl.save()'檢查你的bean的錯誤之後:'println productCtl.errors' – 2013-03-14 15:01:18
或者使用'.save(failOnError:true)'在驗證或保存失敗時拋出異常。 – Bart 2013-03-15 14:43:01