Home arrow Ruby-on-Rails arrow Page 4 - 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 - Creating the Integration Test
(Page 4 of 4 )

First, create the integration test with the generate script:

$ script/generate integration_test Forum

--------------------------------------------
  exists  test/integration/
  create  test/integration/forum_test.rb
--------------------------------------------

If you open the generated test, you can see that it contains one dummy test, which should be removed.

Next, create the new testing DSL for the forum by adding the code shown in Listing 6-3 to test/integration/forum_test.rb .

Listing 6-3. First Version of the Integration Test

require "#{File.dirname(__FILE__)}/../test_helper"

class ForumTest < ActionController::IntegrationTest

  def test_forum
  end

  private

  module ForumTestDSL
    attr_writer :name
  end

  def new_session_as(name)
   
open_session do |session|
      session.extend(ForumTestDSL)
      session.name = name
      yield session if block_given?
   
end
  end

end

The new_session_as method is used for opening a new session for a user. This can be used to simulate George starting his browser and going to the Emporium website, for example. The test_forum method is the main method for the integration test that will use the DSL to test all the user stories as a whole.

Next, add the post_to_forum method to the ForumTestDSL directly after the line attr_writer :name :

def post_to_forum(parameters)
  get "/forum/post"
  assert_response :success
  assert_template "forum/post"

  post "/forum/create", parameters

  assert_response :redirect
  follow_redirect!
  assert_response :success
  assert_template "forum/index"
  return ForumPost.find_by_subject(parameters[:post][:subject])
end

The post_to_forum method simulates a user creating a new post. First, it opens the URL /forum/post and verifies that it works simply by checking the HTTP status code. Then it creates a new post by calling the /forum/post URL. Next, the test verifies that the request was successful and that there is a redirect to the forum main page. At the end, the method returns the post object that was created by the test, so that we can use it later in the test.

Next, put the new method to use by changing the test_forum method as follows:

  def test_forum
   
jill = new_session_as(:jill)
   
post = jill.post_to_forum :post => {
     
:name => 'Bookworm',
     
:subject => 'Downtime',
     
:body => 'Emporium is down again!'
    }
  end

This will test the Post to Forum user story. It simulates Jill creating a new post on the forum. Note that we are saving the post for later use in the integration test, so that we can reply to it.

You can now run the integration test, in true TDD style (it should fail):

$ ruby test/integration/forum_test.rb

--------------------------------------------
Loaded suite test/integration/forum_test Started
F
Finished in 0.061169 seconds.

  1) Failure:
test_forum(ForumTest)

    [test/integration/forum_test.rb:26:in 'post_to_forum'
     test/integration/forum_test.rb:7:in 'test_forum'
     /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/ ?
integration.rb:427:in `run']:
Expected response to be a <:redirect>, but was <200>

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

It fails because we haven't implemented anything but the test yet. Now let's complete the user story by modifying the controller and views that were created by the generate script.

Please check back next week for the third part of this article.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials