上一篇最后出现的错误是因为断言 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">
&nbsp;|&nbsp;
${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(四)的更多相关文章

  1. Play Framework 完整实现一个APP(十四)

    添加测试 ApplicationTest.java @Test public void testAdminSecurity() { Response response = GET("/adm ...

  2. Play Framework 完整实现一个APP(十一)

    添加权限控制 1.导入Secure module,该模块提供了一个controllers.Secure控制器. /conf/application.conf # Import the secure m ...

  3. Play Framework 完整实现一个APP(五)

    程序以及基本可用了,需要继续完善页面 1.创建页面模板 创建文件 app/views/tags/display.html *{ Display a post in one of these modes ...

  4. Play Framework 完整实现一个APP(二)

    1.开发DataModel 在app\moders 下新建User.java package models; import java.util.*; import javax.persistence. ...

  5. Play Framework 完整实现一个APP(十三)

    添加用户编辑区 1.修改Admin.index() public static void index() { List<Post> posts = Post.find("auth ...

  6. Play Framework 完整实现一个APP(十二)

    1.定制CRUD管理页面 > play crud:ov --layout 替换生成文件内容 app/views/CRUD/layout.html #{extends 'admin.html' / ...

  7. Play Framework 完整实现一个APP(十)

    1.定制Comment列表 新增加Comment list页面,执行命令行 > play crud:ov --template Comments/list 会生成/app/views/Comme ...

  8. Play Framework 完整实现一个APP(九)

    添加增删改查操作 1.开启CRUD Module 在/conf/application.conf 中添加 # Import the crud module module.crud=${play.pat ...

  9. Play Framework 完整实现一个APP(八)

    创建Tag标签 1.创建Model @Entity @Table(name = "blog_tag") public class Tag extends Model impleme ...

随机推荐

  1. 深入理解 OWIN 中的 Host 和 Server

    The Open Web Interface for .NET (OWIN),注意单词为大写,之前好像都写成了 Owin,但用于项目的时候,可以写成:Microsoft.Owin.*. OWIN 体系 ...

  2. Matrix Factorization SVD 矩阵分解

    Today we have learned the Matrix Factorization, and I want to record my study notes. Some kownledge ...

  3. C#动态调用WCF接口,两种方式任你选。

    写在前面 接触WCF还是它在最初诞生之处,一个分布式应用的巨作. 从开始接触到现在断断续续,真正使用的项目少之又少,更谈不上深入WCF内部实现机制和原理去研究,最近自己做一个项目时用到了WCF. 从这 ...

  4. 机器学习 1 regression

    Linear regerssion 线性回归 回归: stock market forecast f(过去10年股票起伏的资料) = 明天道琼指数点数 self driving car f(获取的道路 ...

  5. UWP开发入门(二十二)——Storyboard和Animation

    微博上有同学问我MyerSplash是如何实现那个很炫的图片点亮,然后移动到屏幕中央的效果.惭愧啊,我又不是作者哪里会知道.硬着头皮去GitHub拜读了高手的代码,自愧弗如,比我不知道高到哪里去了…… ...

  6. 在ASP.NET Core使用Middleware模拟Custom Error Page功能

    一.使用场景 在传统的ASP.NET MVC中,我们可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAtt ...

  7. 自己实现简单的AOP(二)引入Attribute 为方法指定增强对象

    话续前文 : 自己实现简单的AOP(一)简介 在前一篇文章中,对AOP的实现方式做了一个简单介绍.接下来,引入Attribute 为方法指定增强对象,由此实现一个简单的AOP. 注意:指定的是增强对象 ...

  8. bootstrap-简洁实用的jQuery手风琴插件

    前端 <html lang="zh"> <head> <meta charset="UTF-8"> <meta htt ...

  9. 背水一战 Windows 10 (24) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令

    [源码下载] 背水一战 Windows 10 (24) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令 作者:webabcd ...

  10. Angular2正式版发布,Wijmo抢先支持

    Angular2正式版发布 9月15日,Angular 2 的最终版正式发布了.作为 Angular 1 的全平台继任者 -- Angular 2 的最终版,意味着什么? 意味着稳定性已经得到了大范围 ...