[英]Unit Testing show action of a controller with findwhere in grails
我正在嘗試對控制器的顯示動作進行單元測試。 它的定義是:
def show = {
//scs 11/22/2011 limit access to users in their Organization
def currentOrgViewCheck = session.currentUserOrganizationId.viewAllPost;
def currentOrg = session.currentUserOrganizationId;
def addressesInstance ;
if(currentOrgViewCheck){
addressesInstance = Addresses.findWhere(id:Long.parseLong(params.id));
}else{
addressesInstance = Addresses.findWhere(id:Long.parseLong(params.id), organization:currentOrg);
}
if (!addressesInstance) {
flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'addresses.label', default: 'Addresses'), params.id])}"
redirect(action: "list")
}
else {
[addressesInstance: addressesInstance]
}
}
我試圖通過模擬Addresses域來測試它,然后創建並保存一個Addresses實例,最后將其ID傳遞給控制器的參數。 但是測試沒有通過。
我也嘗試在單元測試方法上測試findWhere方法,無論我給它提供什么參數,它都一直說失蹤方法異常,對於給定的數據類型不存在這樣的簽名。 我完全被困住了。 這是我的測試代碼:
void testShowFound()
{
// New Controller to test
ac = new AddressesController();
// this instance is required both to test as well as a session variable
Organizations org = new Organizations(name:'test', phone: '352-999-8888', createdBy: creator, modifiedBy: modifier, viewAllPost: true);
org.save();
//Create an address to pass its id to the action as parameter.
Addresses a1 = new thlc.Addresses(firstLine:'A1', secondLine:'B', thirdLine:'C', luCountry:UnitedStates, zipCode:'12345', luState:Florida, city:'jag');
a1.save();
ac.metaClass.getParams = {->[id:a1.id] }
//ac.params.id = a1.id; // I tried both ways.
// This findWhere is giving error, can any one explain why ?
//def a = Addresses.findWhere(id:a1.id);
//assertEquals(a, a1);
//session variables mocked.
mockSession['currentUserOrganizationId'] = org;
// This is to bypass the flash message problem, cause it is not possible to
// mock the message method on flash messages.
//ac.metaClass.message = { Map map -> return "ByPass Message" }
//Call the action and test
def model = ac.save();
assert(model);
//Testing redirect, This is the test that is failing
assertEquals("list", redirectParams.action);
}
我已經嘗試了大多數事情,但是無法解決。 我想念什么? 謝謝。
似乎在Grails 1.3.7(及更低版本)中, mockDomain
不會創建findWhere()動態查找器。 從它的聲音來看,如果要使用它,您必須自己模擬該方法。 我還沒有自己做。
參考文獻:
http://www.slideshare.net/tlberglund/test-first-refresh-second-testdriven-development-in-grails (幻燈片38)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.