PlayFramework完整实现一个APP(四)

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

上一篇最后出现的错误是因为断言 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.打开站点

..

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航