• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    Grails中的Controller單元測試

    發布: 2008-10-08 16:15 | 作者: 修行的武者 | 來源: 測試時代采編 | 查看: 103次 | 進入軟件測試論壇討論

    領測軟件測試網

    對于Controller的單元測試,Grails也提供了很方便的支持,使得書寫非常簡單,有代碼為證:

    1. class CategoryControllerTests extends GroovyTestCase {   
    2.        
    3.     void setUp(){   
    4.         def test1= new Category(name: "Test1", parent: null).save()   
    5.         def test2= new Category(name: "Test2", parent: null).save()   
    6.         def test3= new Category(name: "Test3", parent: null).save()   
    7.            
    8.         def test11= new Category(name: "Test11", parent: test1).save()   
    9.         def test12= new Category(name: "Test12", parent: test1).save()   
    10.         def test21= new Category(name: "Test21", parent: test2).save()   
    11.     }   
    12.   
    13.     void testListRoot() {   
    14.         def controller= new CategoryController()
    15.         //categoryList對應返回的model   
    16.         def categoryList= controller.list()?.categoryList   
    17.            
    18.         assertEquals 3, categoryList.size()   
    19.         assertEquals 'Test1', categoryList[0].name   
    20.         assertEquals 'Test2', categoryList[1].name   
    21.         assertEquals 'Test3', categoryList[2].name   
    22.     }   
    23.        
    24.     void testListTest1(){   
    25.         def controller= new CategoryController()
    26.         //其中的params表示的是requestparameter,后面的id是傳入的參數。
    27.         //對于session之類以此類推。   
    28.         controller.params.id= 1
    29.         def categoryList= controller.list()?.categoryList   
    30.            
    31.         assertEquals 2, categoryList.size()   
    32.         assertEquals 'Test11', categoryList[0].name   
    33.         assertEquals 'Test12', categoryList[1].name   
    34.     }   
    35. }   

    Controller部分代碼
    1. class CategoryController {   
    2.     def index = { redirect(action:list,params:params) }   
    3.   
    4.     def allowedMethods = [save:'POST']   
    5.   
    6.     def list = {   
    7.         if(!params.id){   
    8.             return [ categoryList: Category.findAllByParentIsNull() ]   
    9.         }else{   
    10.             def category= Category.get(params.id)   
    11.             if(category){   
    12.                 return [ categoryList: Category.findAll("from Category c where c.parent.id=$params.id"), path: category.getPath()]   
    13.             }else{   
    14.                 flash.message = "Category not found with id ${params.id}"  
    15.                 redirect(action:list)   
    16.             }               
    17.         }   
    18.     }
    19. ......
    20. }  

    以上的代碼基本上向開發者隱藏了背后的Mock機制,使用起來也更加簡單方便。對于如此簡單就能完成Controller的測試,我們沒有理由不把TDD進行到底。

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/

    TAG: controller Grails 單元 Controller


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>