上一篇最后出现的错误是因为断言 assertEquals(1, Post.count()); 出错,取到的Post的数量不是1,运行Test之前,表中有数据
可以添加以下方法,运行Test前清空数据
@Beforepublic void setup() { Fixtures.deleteAll();}
1.编写复杂的测试用例
编辑/test/data.yml
内容替换为 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.*;@OnapplicationStartpublic 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.打开站点
..
新闻热点
疑难解答