grails 2集成测试

时间:2013-03-14 05:57:18

标签: grails integration integration-testing

我在Grails 2.x中创建了一个集成测试,其中包含测试多个控制器的软件,以模拟Web应用程序中的完整用户流。

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

然后,我写了第三种方法来尝试保存产品记录。此记录必须引用类别。因此,我从以前的方法中获取类别实例,并尝试将其传递给其他Product参数中的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)
}

1 个答案:

答案 0 :(得分:0)

两个建议都运作良好。经过数小时和数小时试图弄清楚出了什么问题,谢谢你,我看到了我愚蠢的愚蠢错误:一个字段“shortDesc”被拼错为“shorDesc”。 所以,写了一本关于我书的小提示。 非常感谢你!