国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

PlayFramework完整實現(xiàn)一個APP(四)

2019-11-14 15:16:33
字體:
供稿:網(wǎng)友

 

上一篇最后出現(xiàn)的錯誤是因為斷言 assertEquals(1, Post.count()); 出錯,取到的Post的數(shù)量不是1,運行Test之前,表中有數(shù)據(jù)

可以添加以下方法,運行Test前清空數(shù)據(jù)

@Beforepublic void setup() {	Fixtures.deleteAll();}

  

1.編寫復雜的測試用例

編輯/test/data.yml 

# User(bob):#   email: bob@Gmail.com#   passWord: secret#   fullname: Bob

內(nèi)容替換為 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.初始化數(shù)據(jù)

開始創(chuàng)建應用程序的第一個頁面。這個頁面就會顯示最近的帖子,以及舊的文章的列表。

在開發(fā)第一個屏幕之前我們需要一件事。創(chuàng)建測試數(shù)據(jù)。將默認數(shù)據(jù)注入到博客的一個方法是加載文件在應用程序的加載時間。要做到這一點,我們將創(chuàng)建一個引導工作。

創(chuàng)建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的內(nèi)容,創(chuàng)建的默認數(shù)據(jù)

@OnApplicationStart 標識方法在應用程序啟動時運行

 

3.開發(fā)首頁

修改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.打開站點 

 

..


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 太湖县| 宝鸡市| 财经| 永平县| 拜城县| 福海县| 天全县| 河间市| 军事| 洛阳市| 东台市| 连州市| 峨山| 娄烦县| 斗六市| 榆树市| 五莲县| 南江县| 定边县| 桑日县| 葵青区| 大邑县| 南丰县| 辽宁省| 缙云县| 和政县| 蒙山县| 大余县| 汪清县| 济源市| 米泉市| 浦江县| 邛崃市| 旬邑县| 扬州市| 左云县| 多伦县| 麻阳| 东源县| 汝阳县| 平泉县|