2013-03-14 139 views
-1

我已经在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) 
} 
+0

对不起, 在测试方法“void _03__createProduct()”中断言失败是“assert Product.count()== 1”。这只是最后一个。 谢谢 – donkanmcleod 2013-03-14 14:24:50

+0

'productCtl.save()'检查你的bean的错误之后:'println productCtl.errors' – 2013-03-14 15:01:18

+0

或者使用'.save(failOnError:true)'在验证或保存失败时抛出异常。 – Bart 2013-03-15 14:43:01

回答

0

这两个推荐工作都很好。经过数小时和数小时试图找出哪里出了问题,感谢你,我看到了我的愚蠢愚蠢的错误:一个字段“shortDesc”被拼写为“shorDesc”。所以,我的书中的一点小技巧已经写完了。 非常感谢!