Tagged: testing

Interesting testing issue with Rails 2.3.3

I'm not sure if this applies to all the versions of Rails, but right now I'm using the latest 2.3.3. I'm using shoulda to do testing and my code looks like this:

 context "GET show" do setup do @feed = Factory(:feed) get :show, :id => @feed.to_param end should_not_set_the_flash should_respond_with :success should_render_template :show end 

The method I am testing looks like this:

 def show @feed = Feed.find(params[:id]) @entries = @feed.entries respond_to do |format| format.pjs { debugger; render :template => 'feeds/show', :layout => false } format.json { render :json => @feed.as_json } format.html { render :template => 'feeds/show', :layout => params[:layout] || true } end end 

The result of running this test is: Missing template feeds/show.erb in view path app/views

That is very irritating.

I found that if I change the order as below everything works fine:

 def show @feed = Feed.find(params[:id]) @entries = @feed.entries respond...

Continue Reading →

Using Shoulda to test login – should_require_login

I've had my problems with shoulda, but one very powerful component of the testing framework is the ability to create macros. Here's one that checks to make sure a user has to be logged in to access an action. Put it into test/shoulda_macros/authentication.rb. (You can name the file anything you want I just thought authentication.rb made sense)

 Test::Unit::TestCase.class_eval do def self.should_require_login(*actions) actions.each do |action| should "Require login for '#{action}' action" do get(action) assert_redirected_to(login_url) end end end end 

Then inside your controller test do something like this:

 class UserControllerTest < ActionController::TestCase should_require_login :edit, :update, :destroy end 

Continue Reading →

Tip for Testing Email with Ruby on Rails and Shoulda

Shoulda comes with a great method for testing email called 'assert_sent_email'. (If you try it and get a method missing error update Shoulda. Not that anyone wouldn't know to just do that). No matter what you use to test email as long as you are in test mode your emails will be placed into an array accessible using:


This is due to this line in test.rb found in the environments folder:

 config.action_mailer.delivery_method = :test 

Now here's the tip. You might think you are really smart if you take advantage of the fact that Rails has a very handy initializers folder and put a file in there called mail.rb with your mail configuration.

DON'T do it. The initializer will override the value set in the test.rb file and your tests will fail.

Continue Reading →

Setting up ZenTest and Red Green

I can't take credit for any of these instructions. Dave South from the Logan Ruby group sent these out in an email. I am posting them so that I can find them later on.

Install ZenTest:

sudo gem install ZenTest

Then install RedGreen

sudo gem install redgreen

Go to your rails project directory and create a file called: .autotest

mate .autotest

Add this to the file:

 require 'redgreen/autotest' 

Save and exit

In your terminal, change to your rails application. Instead of typing 'rake' to run the tests, type 'autotest'. It will load the tests and run them all the first time it loads. RedGreen will highlight the result in green if everything passes and red if anything fails. Autotest will continue to run, waiting for changes in the application.

Leave that terminal open and edit your rails application. Whenever you save the app, autotest will detect the save, identify the file(s) that changed, and run appropriate tests.

If you change a model file, for example, aut...

Continue Reading →