Search code examples
ruby-on-rails-3rspec2rspec-rails

trouble getting started with requests testing


Go gentle on me: I've got the flu and only a few of my neural synapses are firing!

Here's a simple requests test for RSpec:

require 'spec_helper'

describe 'Home Page' do
  it 'should mention Home' do
    get '/'
    response.body.should have_content("Home")
  end

end

Great. It works. Now I want to verify that there's an image loaded when visting the home page as well. I assume there's a matcher for images similar to have_content(), so I first go looking for the definition of have_content().

Not found in

https://www.relishapp.com/rspec/search?query=have_content
http://guides.rubyonrails.org/testing.html
http://api.rubyonrails.org/

But the I remember that RSpec has nifty naming rules for matchers, so (e.g.) even?() => be_even(). But even then, searching for "content" in the above doesn't find anything.

(As an aside, I'm pretty sure I'm not looking for the Capybara method of the same name, since I'm doing a get and not visit. Right?)

At the risk of getting this question rejected for being too vague: where the heck is this method coming from, and where do I learn what else I can pass to response.body.should?


Solution

  • RSpec request specs use Capybara, so you can use either get or visit.

    If you want to just check that the page has an <img> element with the correct link, you could use:

    response.should have_selector('img', :src => '...')