-1

我在 Grails 2.x 中创建了一个集成测试,目的是测试几个控制器,以便在 Web 应用程序中模拟完整的用户流程。

首先,我保存了两个简单的域对象记录,在我的测试用例中实例化了两个控制器实例。这两个简单的类是: - 类别(产品的类别)和 - 商店(销售产品的迷你市场)我编写了两种测试方法来控制控制器实例(StoreController 和 CategoryController)并且工作正常,创建了一个类别记录并也有几个店铺记录。

然后,我编写了第三种方法来尝试保存产品记录。该记录必须引用一个类别。因此,我从以前的方法中获取类别实例,并尝试将其传递给 ProductController 以及其他 Product 参数。

由于未知原因,我只能通过在 Web 浏览器中输入数据的 Web 应用程序实例来创建产品记录,而不能通过测试类代码创建产品记录。我认为问题出在 Product 和 Category 之间的关系上,但我不知道应该如何将参数从测试用例传递给 ProductController 以创建新的 Product 记录。

这是我的产品类:

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)
}
4

1 回答 1

0

这两个建议都很好。经过数小时试图弄清楚出了什么问题,多亏了你,我才明白了我愚蠢的错误:字段“shortDesc”被拼错为“shorDesc”。所以,我的书的一个小提示已经写了。非常感谢!

于 2013-03-15T17:24:52.580 回答