Home arrow Ruby-on-Rails arrow Basic Tests for Forum Implementation
RUBY-ON-RAILS

Basic Tests for Forum Implementation


In this second part of a four-part series on setting up a forum in Ruby on Rails, you'll learn how to unit test a model, generate a forum controller and view, and more. This article is excerpted from chapter six of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
May 21, 2010
TABLE OF CONTENTS:
  1. · Basic Tests for Forum Implementation
  2. · Generating the Controller and View
  3. · Implementing the User Stories
  4. · Creating the Integration Test

print this article
SEARCH DEVARTICLES

Basic Tests for Forum Implementation
(Page 1 of 4 )

Unit Testing the Model

It is a good idea to have proper unit tests in place, even if you're using an external plugin that has been tested thoroughly. The plugin API might change in later releases, and this would probably go unnoticed until one of your customers informs you about the problem.

We'll create a basic test that verifies that you can create a post and also create a reply to it. Remember that the generate script has already created an empty unit test for us. All we need to do is add the code shown in Listing 6-2 to test/unit/forum_post_test.rb .

Listing 6-2. Unit Test for the ForumPost Model

require File.dirname(__FILE__) + '/../test_helper'

class ForumPostTest < Test::Unit::TestCase
  fixtures :authors

  def test_create_post_and_reply
   
post = ForumPost.new( :name => 'George',
      :subject => 'Subject',
      :body => 'Body text')

    assert post.save
    assert_not_nil ForumPost.find_by_name('George')

    reply = ForumPost.new(:name => 'Jill',
      :subject => 'Reply',
      :body => 'Reply body text',
      :parent_id => post.id)

    assert reply.save
    assert reply.child?

    post.reload

    assert post.root?
    assert_equal 1, post.all_children().size
    assert_equal reply, post.all_children()[0]
  end
end

Notice that the test uses the authors fixture that we created in Chapter 2. First, the unit test creates a new post, which is the root post of a new thread. Then it saves the post and verifies that it was successfully saved to the database with an assert . Recall that the save method returns false if the object was not saved successfully. We double-check that the post can be found with the find_by_name dynamic finder method (introduced in Chapter 3). The unit test also verifies that the first post is the root post by calling root? . This method returns true if the post has no parent posts.

We also want to be sure that someone can reply to the post, which is why we create a reply to the root post. This is done by creating a new post and setting the parent_id column to the id of the root post. After saving the reply post, we verify that the post really is a reply to the first post by calling the child? method. This method returns true if the post is a child, or reply, of another post.

At the end of the unit test, we verify that the root post has exactly one child post, and that the first post returned by all_children returns the reply.

Before running the tests, clone the development database structure to the test database by executing the following command:

rake db:test:clone_structure

Next, run the unit test as follows:

$ ruby test/unit/forum_post_test.rb

--------------------------------------------
Loaded suite test/unit/forum_post_test Started
.
Finished in 0.101902 seconds.

1 tests, 7 assertions, 0 failures, 0 errors
--------------------------------------------

Now we are ready to create the controller and view.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials