${frontPost.title}
${frontPost.content.nl2br()}
The last error in the previous article was due to an error in the assertion assertEquals(1, Post.count()); and the number of Posts obtained was not 1. Before running the Test, there was data in the table
You can add the following method to run the Test Clear data before
@Before
public void setup() {
Fixtures.deleteAll();
}
1. Write complex test cases
Edit/test/data.yml
# User(bob ):
# email: bob@Gmail.com
# passWord: secret
# fullname: Bob
Replace the content with http://play-framework.herokuapp.com/zh/files/data.yml
Add test cases
@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
.fetch() ;
assertEquals(2, bobPosts.size());
// Find all comments related to Bob's posts
List
"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());
}
Regarding how to use data.yml, you can refer to http://play-framework.herokuapp.com/zh/yaml
2. Initialize data
Start creating the first page of the application. This page will display recent posts, as well as a list of older articles.
We need one thing before developing the first screen. Create test data. One way to inject default data into your blog is to load the file at application load time. To do this, we will create a bootstrap job.
Create 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 uses the content of data.yml to create the default data
@OnApplicationStart identification method to run when the application starts
3. Develop the homepage
Modify the index() method of Application.java
public static void index () {
Post frontPost = Post.find("order by postedAt desc").first();
List
.fetch( 10);
render(frontPost, olderPosts);
}
Modify Application/index.html
#{extends 'main.html' /}
#{set title:'Home' /}
#{if frontPost}
4. Open the site
The above is the content of PlayFramework to completely implement an APP (4). For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!
${oldPost.comments.size()?:'no'}
comment${oldPost.comments.size().pluralize()}
#{if oldPost.comments}
- latest by ${oldPost.comments[0].author}
#{/if}