博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Grails In Action-06.Controlling application flow
阅读量:6828 次
发布时间:2019-06-26

本文共 7069 字,大约阅读时间需要 23 分钟。

hot3.png

第六章主要讲解使用controller实现页面之间的跳转。在系统中实现了Post信息提交及显示功能。主要的知识点有:

  • 访问路径控制:
  • addPost方法:timeline页面的form将表单提交给addPost方法,addPost方法返回
  • .操作符:list.prop
  • ?:操作符:表达式?如果true:如果false
  • ${flash.massage}
  • spock单元测试:controller页面重定向测试
  • spock单元测试:@spock.lang.Unroll
  • spock单元测试:"Testing id of #suppliedId redirects to #expectedUrl"
  • spock集成测试

默认访问index,重定向给timeline/params,如果没有params,默认给一个.

......def index() { if (!params.id) params.id = "jeff" redirect action: 'timeline', params: params }def timeline() { def user = User.findByLoginId(params.id) if (!user) { response.sendError(404) } else { [ user : user ] }}......

增加一个post,因为post需要指定user,所以需要user登录,前面的index指定了一个user params,可以直接使用.后续增加登录功能

......def addPost() { def user = User.findByLoginId(params.id) if (user) { def post = new Post(params) user.addToPosts(post) if (user.save()) { flash.message = "Successfully created Post" } else { flash.message = "Invalid or empty post" } } else { flash.message = "Invalid User Id" } redirect(action: 'timeline', id: params.id)}......

访问index,重定向给timeline,timeline.gsp负责提交post并显示post信息,所以,timeline分这两个部分.

Timeline for ${ user.profile ? user.profile.fullName : user.loginId }

What is ${ user.profile ? user.profile.fullName : user.loginId } hacking on right now?

${flash.message}
<@!-- 提交(addPost) -->

<@!-- 显示(timeline/params) -->
${post.content}

这本书在测试上下了一些功夫,讲解的也很到位. 单元测试:test/unit/com.grailsinaction.PostControllerSpec.groovy

package com.grailsinactionimport grails.test.mixin.Mockimport grails.test.mixin.TestForimport spock.lang.Specification@TestFor(PostController)@Mock([User,Post])class PostControllerSpec extends Specification { def "Get a users timeline given their id"() { given: "A user with posts in the db" User chuck = new User(loginId: "chuck_norris", password: "password").save(failOnError: true) chuck.addToPosts(new Post(content: "A first post")) chuck.addToPosts(new Post(content: "A second post")) and: "A loginId parameter" params.id = chuck.loginId when: "the timeline is invoked" def model = controller.timeline() then: "the user is in the returned model" model.user.loginId == "chuck_norris" model.user.posts.size() == 2 } def "Check that non-existent users are handled with an error"() { given: "the id of a non-existent user" params.id = "this-user-id-does-not-exist" when: "the timeline is invoked" controller.timeline() then: "a 404 is sent to the browser" response.status == 404 } def "Adding a valid new post to the timeline"() { given: "A user with posts in the db" User chuck = new User(loginId: "chuck_norris", password: "password").save(failOnError: true) and: "A loginId parameter" params.id = chuck.loginId and: "Some content for the post" params.content = "Chuck Norris can unit test entire applications with a single assert." when: "addPost is invoked" def model = controller.addPost() then: "our flash message and redirect confirms the success" flash.message == "Successfully created Post" response.redirectedUrl == "/post/timeline/${chuck.loginId}" Post.countByUser(chuck) == 1 } def "Adding a invalid new post to the timeline trips an error"() { given: "A user with posts in the db" User chuck = new User(loginId: "chuck_norris", password: "password").save(failOnError: true) and: "A loginId parameter" params.id = chuck.loginId and: "Some content for the post" params.content = null when: "addPost is invoked" def model = controller.addPost() then: "our flash message and redirect confirms the success" flash.message == "Invalid or empty post" response.redirectedUrl == "/post/timeline/${chuck.loginId}" Post.countByUser(chuck) == 0 } @spock.lang.Unroll def "Testing id of #suppliedId redirects to #expectedUrl"() { given: params.id = suppliedId when: "Controller is invoked" controller.index() then: response.redirectedUrl == expectedUrl where: suppliedId | expectedUrl 'joe_cool' | '/post/timeline/joe_cool' null | '/post/timeline/chuck_norris' }}

集成测试:test/integration/com.grailsinaction.PostIntegrationSpec.groovy

package com.grailsinactionimport grails.plugin.spock.IntegrationSpecclass PostIntegrationSpec extends IntegrationSpec { def "Adding posts to user links post to user"() { given: "A brand new user" def user = new User(loginId: 'joe', password: 'secret').save(failOnError: true) when: "Several posts are added to the user" user.addToPosts(new Post(content: "First post... W00t!")) user.addToPosts(new Post(content: "Second post...")) user.addToPosts(new Post(content: "Third post...")) then: "The user has a list of posts attached" 3 == User.get(user.id).posts.size() } def "Ensure posts linked to a user can be retrieved"() { given: "A user with several posts" def user = new User(loginId: 'joe', password: 'secret').save(failOnError: true) user.addToPosts(new Post(content: "First")) user.addToPosts(new Post(content: "Second")) user.addToPosts(new Post(content: "Third")) when: "The user is retrieved by their id" def foundUser = User.get(user.id) List
sortedPostContent = foundUser.posts.collect { it.content }.sort() then: "The posts appear on the retrieved user" sortedPostContent == ['First', 'Second', 'Third'] } def "Exercise tagging several posts with various tags"() { given: "A user with a set of tags" def user = new User(loginId: 'joe', password: 'secret').save(failOnError: true) def tagGroovy = new Tag(name: 'groovy') def tagGrails = new Tag(name: 'grails') user.addToTags(tagGroovy) user.addToTags(tagGrails) when: "The user tags two fresh posts" def groovyPost = new Post(content: "A groovy post") user.addToPosts(groovyPost) groovyPost.addToTags(tagGroovy) def bothPost = new Post(content: "A groovy and grails post") user.addToPosts(bothPost) bothPost.addToTags(tagGroovy) bothPost.addToTags(tagGrails) then: user.tags*.name.sort() == ['grails', 'groovy'] 1 == groovyPost.tags.size() 2 == bothPost.tags.size() }}

转载于:https://my.oschina.net/65304586/blog/179097

你可能感兴趣的文章
RHEL6.5的安装过程
查看>>
js随笔
查看>>
Java逆向基础之打印所有类名
查看>>
Andrdoid Sensors Overview (探测器概述)二
查看>>
使用screen实现字符桌面共享
查看>>
css hack的认识
查看>>
photoshop制作透明背景图片
查看>>
通过vftps和虚拟帐号增强ftp的安全性
查看>>
HBase Filter使用方法(三)------批量put导入
查看>>
CentOS下httpd与tomcat整合
查看>>
oracle group by 行转列 分析执行计划
查看>>
ubuntu10.04基本设置
查看>>
SCCM Deploy Microsoft Teams
查看>>
领域模型设计的见解
查看>>
linux命令:pwd
查看>>
linux用户权限相关内容查看
查看>>
孤陋寡闻之不用电池的阵列卡
查看>>
linux虚拟化之openvz
查看>>
Java那些坑(1):Java7的substring
查看>>
我的友情链接
查看>>