View Specs

Hi,

I was using WebRat and I had view specs that looked like this:

describe “expenses/new.html.erb” do
it “displays ‘New Expense’” do
render
rendered.should include(“New Expense”)
end
end

I am now using Capybara and you cannot use those matchers in View
Specs.
What should my view specs look like?

If I try using: get “expenses/edit”
I end up with: undefined method `get’ for
#RSpec::Core::ExampleGroup::Nested_1:0xaf364ac

Am I meant to keep using render, rendered?
How should the spec above be re-written?
And the nested one below?

it “renders a form to create an expense” do
render
rendered.should have_selector(“form”,
:method => “post”,
:action => expenses_path
) do |form|

    form.should have_selector("input",
      :type => "text",
      :name => "expense[name]")

    form.should have_selector("input",
      :type => "text",
      :name => "expense[limit]")

    form.should have_selector("input",
      :type => "submit",
      :value => "Add")

    form.should have_selector("a",
        :content => "Cancel",
        :href => expenses_path)
  end
end

Thanks, Daryn

On Wed, Dec 22, 2010 at 12:58 PM, Daryn [email protected] wrote:

How should the spec above be re-written?
:type => “text”,
form.should have_selector(“a”,
:content => “Cancel”,
:href => expenses_path)
end
end

render(), rendered(), and the include() matcher all come from rspec or
rails, not Webrat or Capybara.

get() is from rails, but only used in controller and request specs
(which are thin wrappers for rails’ functional and integration tests).

The only thing you’ve lost for now is Webrat’s have_selector()
matcher. For right now your best bet is rails’ assert_select:

http://api.rubyonrails.org/classes/ActionDispatch/Assertions/SelectorAssertions.html#method-i-assert_select

Sometime soon, Capybara’s counterparts, have_selector() and
have_xpath() will also be available in view specs. How soon, I’m not
sure, but Jonas and I worked on this a bit together last month at
RubyConf.

HTH,
David