Chowlett
Chowlett

Reputation: 46677

Recommended testing frameworks for Rails 2 app with randomness

I have a Rails 2.3.5 app which is serving a card game. I've been a bit lax in writing tests for it (read: I haven't written any :embarrassed:), and I'd like to make a start now.

From reading other questions, I think I probably want to be using Shoulda extending Test::Unit for unit testing.

I wondered about using Capybara extending RSpec for functional testing, but the majority of users' interaction with the app is via POST, which I understand Capybara doesn't handle.

Since this is a card game, I obviously need the ability to control rand; I also definitely need the framework to handle Javascript.

  1. Is Test::Unit + Shoulda suitable?
  2. Am I correct that Capybara can't handle POST? Can I work around that?
  3. Is there a better option instead of Capybara?
  4. Can these methods handle the randomness involved in a card-game? Presumably, for Test::Unit at least, I can just call srand at some early stage to fix the generator, but I don't know if that's doable for Capybara/RSpec/anything else.
  5. Can anyone point me to resources dealing specifically with setting up Shoulda, Capybara and RSpec for Rails 2.3.5? It's a little hard to tell what's Rails 3 specific and what isn't.
  6. Would I gain anything from adding Watir / Firewatir into the mix?

I realise the above questions are manifold, but they basically boil down to "does this work, or have you any better suggestions?"

Upvotes: 0

Views: 310

Answers (1)

Dan Croak
Dan Croak

Reputation: 1669

If the majority of the users' interactions are via POST, is it via an API (as opposed to filling out forms or something)?

Just about any combination of RSpec/Shoulda/Capybara/Test Unit/Rack::Test could work for you depending on your need. They're all capable. However, these are my recommendations:

  • If you've decided you want integration testing via HTML pages, use Cucumber and Capybara
  • If you've decided you want integration testing via HTTP API, use RSpec and Rack::Test
  • You probably want to fake out randomness.
  • You probably don't need Watir/Firewatir.

It does look like you can make POST requests via some Capybara drivers:

http://suffix.be/blog/capybara-post-requests

When Rails moved to 3.0, RSpec went to 2.0 so for at least RSpec, you'd want RSpec and RSpec Rails 1.3.2.

By "fake out randomess", I mean redefine srand in your tests so you can predictably run them.

module Kernel
  def rand
    YourApp.rand
  end
end

module MyApp
  class << self
    attr_accessor :rand
  end
end

Then, before you have the user press the button, run a step definition like "When random returns 6", which will set MyApp.rand = 6.

Upvotes: 2

Related Questions