views:

415

answers:

7

I am testing a Ruby Rails website and wanted to get started with Unit and Functional testing.

A: 

http://wiki.rubyonrails.org/rails/pages/HowToDoTestDrivenDevelopmentInRails

Beyond this, you should tell us what you've tried. Or what you need to do that's special. Generic answers are easy to find.

Glenn
+5  A: 

Cucumber and RSpec are worth a look. They encourage testing in a behaviour-driven, example-based style.

RSpec is a library for unit-level testing:

describe "hello_world"
  it "should say hello to the world" do
    # RSpec comes with its own mock-object framework built in,
    # though it lets you use others if you prefer
    world = mock("World", :population => 6e9)
    world.should_receive(:hello)
    hello_world(world)
  end
end

It has special support for Rails (e.g. it can test models, views and controllers in isolation) and can replace the testing mechanisms built in to Rails.

Cucumber (formerly known as the RSpec Story Runner) lets you write high-level acceptance tests in (fairly) plain English that you could show to (and agree with) a customer, then run them:

Story: Commenting on articles

  As a visitor to the blog
  I want to post comments on articles
  So that I can have my 15 minutes of fame

  Scenario: Post a new comment

    Given I am viewing an article
    When I add a comment "Me too!"
    And I fill in the CAPTCHA correctly
    Then I should see a comment "Me too!"
Sam Stokes
The second API for acceptance tests has been taken out of RSpec and is now called "cucumber".
Christian Lescuyer
Thanks - I've edited to bring the answer up to date.
Sam Stokes
+3  A: 

My recommendation is (seriously) just bypass the built in rails unit/functional testing stuff, and go straight for RSpec.

The built in rails stuff uses the Test::Unit framework which ships with ruby, and which is more or less a straight port of JUnit/NUnit/AnyOtherUnit.
I found these frameworks all rather tedious and annoying, leading to general apathy about writing unit tests, which is obviously not what you're trying to acheive here.

RSpec is a different beast, being centered around describing what your code should do, rather than asserting what it already does. It will change how you view testing, and you'll have a heck of a lot more fun doing it.

If I sound like a bit of a fanboy, it's only because I really believe RSpec is that good. I went from being annoyed and tired of unit/functional testing, to a staunch believer in it, pretty much solely because of rspec.

Orion Edwards
Test::Unit and RSpec are just tools -- the value is in how you use them. Anything you can do in RSpec you can do in Test::Unit.BDD is an improvement over TDD only because it points out in clear language what you should have been doing in your tests anyway.See http://dannorth.net/introducing-bdd
Ian Terrell
which is exactly my point. I could write all my tests in x86 assembler if I wanted. The value is in how it affects your thinking, and therefore helps you to produce better tests
Orion Edwards
+2  A: 

It sounds like you've already written your application, so I'm not sure you'll get a huge bonus from using RSpec over Test::Unit.

Anyhow regardless of which one you choose, you'll quickly run into another issue: managing fixtures and mocks (i.e. your test "data"). So take a look at Shoulda and Factory Girl.

Dave Nolan
A: 

Rick Bradley gave a talk at Ruby Hoedown 2008 on precisely this topic. You can get the video at confreaks. If you are not that familiar with mock objects I also recommend Joe O'Brien and Jim Weirich's presentation about flex mock.

srboisvert
A: 

Even if the app is already written, I would recommend using RSpec over Test::Unit for the simple fact that no application is ever finished. You're going to want to add features and refactor code. Getting the right test habit early on will help make these alterations less painful

Aaron
+1  A: 

You can also test out the web interface with a Firefox plug-in such as http://selenium.openqa.org/

It will record clicks and text entry and then plays back and will check the page for the correct display elements.

danivovich