可以添加以下方法,运行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.打开站点
以上就是playframework完整实现一个app(四)的内容。
