上一篇最后出現的錯誤是因為斷言 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.打開站點
..
新聞熱點
疑難解答