Play Framework 完整实现一个APP(四)
上一篇最后出现的错误是因为断言 assertEquals(1, Post.count()); 出错,取到的Post的数量不是1,运行Test之前,表中有数据
可以添加以下方法,运行Test前清空数据
@Before
public void setup() {
Fixtures.deleteAll();
}
1.编写复杂的测试用例
编辑/test/data.yml
# User(bob):
# email: bob@gmail.com
# password: secret
# fullname: Bob
内容替换为 http://play-framework.herokuapp.com/zh/files/data.yml
添加测试用例
@Test
public void fullTest() {
Fixtures.loadModels("data.yml"); // Count things
assertEquals(2, User.count());
assertEquals(3, Post.count());
assertEquals(3, Comment.count()); // Try to connect as users
assertNotNull(User.connect("bob@gmail.com", "secret"));
assertNotNull(User.connect("jeff@gmail.com", "secret"));
assertNull(User.connect("jeff@gmail.com", "badpassword"));
assertNull(User.connect("tom@gmail.com", "secret")); // Find all of Bob's posts
List<Post> bobPosts = Post.find("author.email", "bob@gmail.com")
.fetch();
assertEquals(2, bobPosts.size()); // Find all comments related to Bob's posts
List<Comment> bobComments = Comment.find("post.author.email",
"bob@gmail.com").fetch();
assertEquals(3, bobComments.size()); // Find the most recent post
Post frontPost = Post.find("order by postedAt desc").first();
assertNotNull(frontPost);
assertEquals("About the model layer", frontPost.title); // Check that this post has two comments
assertEquals(2, frontPost.comments.size()); // Post a new comment
frontPost.addComment("Jim", "Hello guys");
assertEquals(3, frontPost.comments.size());
assertEquals(4, Comment.count());
}
关于如何使用 data.yml,可以参考 http://play-framework.herokuapp.com/zh/yaml
2.初始化数据
开始创建应用程序的第一个页面。这个页面就会显示最近的帖子,以及旧的文章的列表。
在开发第一个屏幕之前我们需要一件事。创建测试数据。将默认数据注入到博客的一个方法是加载文件在应用程序的加载时间。要做到这一点,我们将创建一个引导工作。
创建Bootstrap.java
package models; import play.*;
import play.jobs.*;
import play.test.*; @OnApplicationStart
public class Bootstrap extends Job {
public void doJob() {
// Check if the database is empty
if (User.count() == 0) {
Fixtures.loadModels("initial-data.yml");
}
}
}
initial-data.yml 使用data.yml的内容,创建的默认数据
@OnApplicationStart 标识方法在应用程序启动时运行
3.开发首页
修改Application.java 的index()方法
public static void index() {
Post frontPost = Post.find("order by postedAt desc").first();
List<Post> olderPosts = Post.find("order by postedAt desc").from(1)
.fetch(10);
render(frontPost, olderPosts);
}
修改Application/index.html
#{extends 'main.html' /}
#{set title:'Home' /}
#{if frontPost}
<div class="post">
<h2 class="post-title">
<a href="#">${frontPost.title}</a>
</h2>
<div class="post-metadata">
<span class="post-author">by ${frontPost.author.fullname}</span>
<span class="post-data">by ${frontPost.postedAt.format('MMM dd')}</span>
<span class="post-comments">
|
${frontPost.comments.size()?:'no'}
comment${frontPost.comments.size().pluralize()}
#{if frontPost.comments}
, latest by ${frontPost.comments[0].author}
#{/if}
</span>
</div>
<div class="post-content">
${frontPost.content.nl2br()}
</div>
</div>
#{if olderPosts.size()>1}
<div class="older-posts">
<h3>Older posts <span class="from">from this blog</span></h3>
#{list items:olderPosts, as:'oldPost'}
<div class="post">
<h2 class="post-title">
<a href="#">${oldPost.title}</a>
</h2>
<div class="post-metadata">
<span class="post-author">
by ${oldPost.author.fullname}
</span>
<span class="post-date">
${oldPost.postedAt.format('dd MMM yy')}
</span>
<div class="post-comments">
${oldPost.comments.size()?:'no'}
comment${oldPost.comments.size().pluralize()}
#{if oldPost.comments}
- latest by ${oldPost.comments[0].author}
#{/if}
</div>
</div>
</div>
#{/list}
</div>
#{/if}
#{/if}
#{else}
<div class="empty">
There is currently nothing to read here.
</div>
#{/else}
4.打开站点

..
Play Framework 完整实现一个APP(四)的更多相关文章
- Play Framework 完整实现一个APP(十四)
添加测试 ApplicationTest.java @Test public void testAdminSecurity() { Response response = GET("/adm ...
- Play Framework 完整实现一个APP(十一)
添加权限控制 1.导入Secure module,该模块提供了一个controllers.Secure控制器. /conf/application.conf # Import the secure m ...
- Play Framework 完整实现一个APP(五)
程序以及基本可用了,需要继续完善页面 1.创建页面模板 创建文件 app/views/tags/display.html *{ Display a post in one of these modes ...
- Play Framework 完整实现一个APP(二)
1.开发DataModel 在app\moders 下新建User.java package models; import java.util.*; import javax.persistence. ...
- Play Framework 完整实现一个APP(十三)
添加用户编辑区 1.修改Admin.index() public static void index() { List<Post> posts = Post.find("auth ...
- Play Framework 完整实现一个APP(十二)
1.定制CRUD管理页面 > play crud:ov --layout 替换生成文件内容 app/views/CRUD/layout.html #{extends 'admin.html' / ...
- Play Framework 完整实现一个APP(十)
1.定制Comment列表 新增加Comment list页面,执行命令行 > play crud:ov --template Comments/list 会生成/app/views/Comme ...
- Play Framework 完整实现一个APP(九)
添加增删改查操作 1.开启CRUD Module 在/conf/application.conf 中添加 # Import the crud module module.crud=${play.pat ...
- Play Framework 完整实现一个APP(八)
创建Tag标签 1.创建Model @Entity @Table(name = "blog_tag") public class Tag extends Model impleme ...
随机推荐
- 你真的了解load方法么?(转载)
本文授权转载,作者:左书祺(关注仓库,及时获得更新:iOS-Source-Code-Analyze) 因为 ObjC 的 runtime 只能在 Mac OS 下才能编译,所以文章中的代码都是在 Ma ...
- 安装infer整个过程
日期:2015-06-26 孟起 15:43:25 大神.. 孟起 15:43:38 我是不是照着这个安装 HelloWorld 15:45:05 直接找二进制文件安卓就行 孟起 15:46: ...
- Spring整合Ehcache管理缓存
前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...
- HTTP请求方法详解
HTTP请求方法详解 请求方法:指定了客户端想对指定的资源/服务器作何种操作 下面我们介绍HTTP/1.1中可用的请求方法: [GET:获取资源] GET方法用来请求已被URI识别的资源.指定 ...
- [Asp.net 5] Logging-新日志系统目录
楼主有个美好的愿望——把asp.net 5所有能看懂的代码一一呈现给大家(比如C++,楼主就看不懂).现在已经做完了依赖注入.多语言.配置文件三部分,比较基础的日志就成为了楼主的下一个目标.下面是楼主 ...
- Android网络编程1
最近在自学Android开发,从这篇开始作为我学习android开发的笔记,来记录学习过程中遇到的问题点和其解决的方法: Ui界面代码 <?xml version="1.0" ...
- clientTarget与用户代理别名
将特定用户代理的别名添加到用户代理别名的内部集合中. 来自 <https://msdn.microsoft.com/zh-cn/library/6379d90d(v=vs.110).aspx&g ...
- roleManager与角色管理授权
总览地址 https://msdn.microsoft.com/zh-cn/library/9ab2fxh0.aspx 其中基本概述是第一篇:了解角色管理 来自 <https://msdn.mi ...
- 浅谈 C#委托
看了<CLR via C#>的17章委托后,为自己做一点浅显的总结,也分享给需要的人. .NET通过委托来提供一种回调函数机制,.NET委托提供了很多功能,例如确保回调方法是类型安全的(C ...
- Hibernate框架中Criteria语句
在Hibernate中有一种查询语句是Criteria查询(QBC查询),今天呢 我们就一个个的详细的跟大家一起探讨Criteria语句的相关知识点 案例前的准备 //插入测试数据,构建数据库 pub ...