|
View:
New views
6 Messages
—
Rating Filter:
Alert me
|
|
|
Definitive Guide To Grails (2nd Ed.): Chapter 4 exemple issueHello all,
I'm currently reading the excellent book The Definitive Guide To Grails (Second Edition). Since the end of the 4th chapter about controllers and tests, I'm facing some issues. Here is the context: # The Domain Class: Users.groovy package com.g2one.gtunes class Users { String login String password String firstName String lastName static hasMany = [purchasedSongs:Song] static constraints = { login blank:false, size:5..15,matches:/[\S]+/, unique:true password blank:false,size:5..15,matches:/[\S]+/ firstName blank:false lastName blank:false } } # The Controller: UsersController.groovy class UsersController { def register = { if(request.method == 'POST') { def u = new Users(params) if(u.password != params.confirm) { u.errors.rejectValue("password", "users.password.dontmatch") return [users:u] } else if(u.save()) { session.users = u redirect(controller:"store") } else { return [users:u] } } } def login = {LoginCommand cmd -> if(request.method == 'POST'){ if(!cmd.hasErrors()){ session.users = cmd.getUser() redirect(controller:'store') } else{ render(view:'/store/index', model:[loginCmd:cmd]) } }else{ render(view:'/store/index') } } } and the corresponding command in the same file class LoginCommand { String login String password private u Users getUser(){ if(!u && login) u = Users.findByLogin(login, [fetch:[purchasedSongs: 'join']]) return u } static contraints = { login blank:false, validator:{ val, obj -> if(!obj.users) return "users.not.found" } password blank:false, validator:{ val, obj -> if(obj.users && obj.users.password != val) return "users.password.invalid" } } } # The Unit Test: UsersControllerTests.groovy package com.g2one.gtunes import grails.test.* class UsersControllerTests extends ControllerUnitTestCase { protected void setUp() { super.setUp() } protected void tearDown() { super.tearDown() } void testPasswordMatch() { mockRequest.method = 'POST' mockDomain(Users) mockParams.login = "joebloggs" mockParams.password = "password" mockParams.confirm = "different" mockParams.firstName = "Joe" mockParams.lastName = "Blogs" def model = controller.register() assert model?.users def users = model.users assert users.hasErrors() assertEquals "users.password.dontmatch", users.errors.password } void testRegistrationFailed(){ mockRequest.method = 'POST' mockDomain(Users) mockParams.login = "" def model = controller.register() assertNull mockSession.user assert model def users = model.users assert users.hasErrors() assertEquals "blank", users.errors.login assertEquals "nullable", users.errors.password assertEquals "nullable", users.errors.firstName assertEquals "nullable", users.errors.lastName } void testRegistrationSuccess(){ mockRequest.method = 'POST' mockDomain(Users) mockParams.login = "joebloggs" mockParams.password = "password" mockParams.confirm = "password" mockParams.firstName = "Joe" mockParams.lastName = "Blogs" def model = controller.register() assertEquals 'store', redirectArgs.controller assertNotNull mockSession.users } void testLoginUserNotFound(){ mockRequest.method = 'POST' mockDomain(Users) MockUtils.prepareForConstraintsTests(LoginCommand) def cmd = new LoginCommand(login:"fred", password:"letmein") cmd.validate() controller.login(cmd) assertTrue cmd.hasErrors() assertEquals "users.not.found", cmd.errors.login assertEquals "/store/index", renderArgs.view } void testLoginPasswordInvalid(){ mockRequest.method = 'POST' def toto = mockDomain(Users, [new Users( login:"fred", password:"realpassword", lastName:"frederic", firstName:"Graeme")]) def u = Users.findByLogin('fred', [fetch:[purchasedSongs: 'join']]) println u.getProperties() MockUtils.prepareForConstraintsTests(LoginCommand) def cmd = new LoginCommand(login:"Fred", password:"letmein") cmd.validate() controller.login(cmd) assertTrue cmd.hasErrors() assertEquals "users.password.invalid", cmd.errors.password assertEquals "/store/index", renderArgs.view } void testLoginSuccess(){ mockRequest.method = 'POST' mockDomain(Users, [new Users(login:"Fred", password:"letmein")]) MockUtils.prepareForConstraintsTests(LoginCommand) def cmd = new LoginCommand(login:"Fred", password:"letmein") cmd.validate() controller.login(cmd) assertFalse cmd.hasErrors() assertNotNull mockSession.users assertEquals "store", redirectArgs.controller } } Here is the tests result (extract) when I run: grails test-app Testcase: testLoginUserNotFound took 0,16 sec FAILED null junit.framework.AssertionFailedError at junit.framework.Assert$assertTrue.callCurrent(Unknown Source) at com.g2one.gtunes.UsersControllerTests.testLoginUserNotFound(UsersControllerTests.groovy:72) at _GrailsTest_groovy$_run_closure4.doCall(_GrailsTest_groovy:203) at _GrailsTest_groovy$_run_closure4.call(_GrailsTest_groovy) at _GrailsTest_groovy$_run_closure2.doCall(_GrailsTest_groovy:147) at _GrailsTest_groovy$_run_closure1_closure19.doCall(_GrailsTest_groovy:113) at _GrailsTest_groovy$_run_closure1.doCall(_GrailsTest_groovy:96) at TestApp$_run_closure1.doCall(TestApp.groovy:66) at gant.Gant$_dispatch_closure4.doCall(Gant.groovy:324) at gant.Gant$_dispatch_closure6.doCall(Gant.groovy:334) at gant.Gant$_dispatch_closure6.doCall(Gant.groovy) at gant.Gant.withBuildListeners(Gant.groovy:344) at gant.Gant.this$2$withBuildListeners(Gant.groovy) at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source) at gant.Gant.dispatch(Gant.groovy:334) at gant.Gant.this$2$dispatch(Gant.groovy) at gant.Gant.invokeMethod(Gant.groovy) at gant.Gant.processTargets(Gant.groovy:495) at gant.Gant.processTargets(Gant.groovy:480) Testcase: testLoginPasswordInvalid took 0,063 sec FAILED null junit.framework.AssertionFailedError at junit.framework.Assert$assertTrue.callCurrent(Unknown Source) at com.g2one.gtunes.UsersControllerTests.testLoginPasswordInvalid(UsersControllerTests.groovy:91) at _GrailsTest_groovy$_run_closure4.doCall(_GrailsTest_groovy:203) at _GrailsTest_groovy$_run_closure4.call(_GrailsTest_groovy) at _GrailsTest_groovy$_run_closure2.doCall(_GrailsTest_groovy:147) at _GrailsTest_groovy$_run_closure1_closure19.doCall(_GrailsTest_groovy:113) at _GrailsTest_groovy$_run_closure1.doCall(_GrailsTest_groovy:96) at TestApp$_run_closure1.doCall(TestApp.groovy:66) at gant.Gant$_dispatch_closure4.doCall(Gant.groovy:324) at gant.Gant$_dispatch_closure6.doCall(Gant.groovy:334) at gant.Gant$_dispatch_closure6.doCall(Gant.groovy) at gant.Gant.withBuildListeners(Gant.groovy:344) at gant.Gant.this$2$withBuildListeners(Gant.groovy) at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source) at gant.Gant.dispatch(Gant.groovy:334) at gant.Gant.this$2$dispatch(Gant.groovy) at gant.Gant.invokeMethod(Gant.groovy) at gant.Gant.processTargets(Gant.groovy:495) at gant.Gant.processTargets(Gant.groovy:480) Testcase: testLoginSuccess took 0,073 sec So, testLoginUserNotFound and testLoginPasswordInvalid failed. Notice that those are faillures and not errors... I make my discreet inquiries and I think both cmd.validate() return no error and consequently assertTrue cmd.hasErrors() fail. I notice that in the command, u is always null and the customs validators never return anything. I don't know if I'm on the good way but as I'm a newbee in Groovy and Grails I think I've reach my limits. Last thing and maybe not the least, I do not understand the 'obj.users' object in the validator. It should be more logic if it was 'obj.u'. Can you provide me some help/explainations/clarification on those issues ? Regards, Romain Ps: I am using Groovy 1.6.5 & Grails 1.1.1 on Linux 2.6.x (ArchLinux) with Netbeans 6.7.1 |
|
|
Re: Definitive Guide To Grails (2nd Ed.): Chapter 4 exemple issuethere seems to be an error in your command object, I would expect the
method to be called getUsers() for the rest of your code to work. if you think of obj.users as a shortcut for obj.getUsers() , it should make sense. T > Last thing and maybe not the least, I do not > understand the 'obj.users' object in the validator. It should be more logic > if it was 'obj.u'. Can you provide me some help/explainations/clarification > on those issues ? > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: Definitive Guide To Grails (2nd Ed.): Chapter 4 exemple issueHi Tomas,
Thank you for your response. To test I tried to replace my command from obj.users to obj.getUsers() but nothing change. The two tests return exactly the same result. Anyone has another idea of what happen? Romain
|
|
|
Re: Definitive Guide To Grails (2nd Ed.): Chapter 4 exemple issue Users getUsers(){
if(!u && login) u = Users.findByLogin(login, [fetch:[purchasedSongs: 'join']]) return u } Try that? On Fri, Nov 6, 2009 at 8:02 AM, KaBooFa <romain.bourguignon@...> wrote:
|
|
|
Re: Definitive Guide To Grails (2nd Ed.): Chapter 4 exemple issueoh, yes I saw this mistake and I've already tried :) Without result
Thank you
|
|
|
Re: Definitive Guide To Grails (2nd Ed.): Chapter 4 exemple issueHello All,
I'm still having the problem. Can someone else provides me help? I'm feeling alone... Regards, Romain
|
| Free embeddable forum powered by Nabble | Forum Help |