0x4a6f4672
0x4a6f4672

Reputation: 28245

RSpec View testing with Rails3 and RSpec2

RSpec2 does not include an have_tag test helper. Using webrat's have_tag or have_selector matchers instead is not possible because Webrat and Rails 3 are not compatible yet. Is there a way to write useful RSpec view tests? It is possible to use assert_select instead of have_tag, but then one could Test::Unit tests in the first place. Or is it no longer recommendable to write RSpec view tests, because integration tests with Capybara or Cucumber are better?

Upvotes: 0

Views: 859

Answers (2)

0x4a6f4672
0x4a6f4672

Reputation: 28245

Webrat caused too much trouble, it is also possible to use Capybara with RSpec. The Capybara DSL (with the functions has_selector?, has_content?, etc.) is available for the following RSpec tests: spec/requests, spec/acceptance, or spec/integration.

If you use the latest version of Capybara (~> 1.0.1) - older versions like 0.4.0 won't support this - and add the following lines to your spec_helper.rb file

require "capybara/rspec"
require "capybara/rails"

then you could write for example the following RSpec request test

require 'spec_helper'

describe "Posts" do
  describe "GET /blog" do
    it "should get blog posts" do
      get blog_path
      response.status.should be(200)
      response.body.should have_selector "div#blog_header"
      response.body.should have_selector "div#blog_posts"
    end
  end
end

Upvotes: 0

mikong
mikong

Reputation: 8370

Actually, Webrat works with Rails 3. I have tested this and I was able to use the have_selector matcher (have_tag didn't work).

You can take a look at this Google group discussion. Basically, you don't need the Webrat.configure block mentioned in the webrat readme, and following the mailing list solution, add these lines in your spec_helper.rb:

include Webrat::Methods
include Webrat::Matchers

As you can see, Webrat is not so updated anymore, so yes, you might be better off with integration testing with Cucumber (+ Capybara).

Upvotes: 1

Related Questions