4

I'm getting started with rspec, capybara, etc, and I want to do some test driven development in Rails.

The specification I'm working with has very precise definitions for the appearance of the headers and the footers, and I figure that these are a good place to start learning.

For the footer, I want to have the following rule:

If the user is logged in, then the header contains just a logo image. The logo image should be a link to the user's landing page, which is determined by the rights the user has. If the user is not logged in, then the image is not a link, and four other links should appear in the footer as well, in a table.

Coding this is actually fairly straightfoward in erb, but I'm trying to Do The Right Thing, and make a series of tests here. My problems is that I can't seem to be able to test whether or not an image is shown on the screen. I've read the rspec book, but I don't see where it says something like 'shows this image found in the assets directory.'

So my setup is, in the views/layouts directory, _header.html.erb _footer.html.erb _shim.html.erb application.html.erb

I would think that I could test the footer partial directly, using something like:

require "spec_helper"

describe "rendering views/layouts/_footer.html.erb" do
  #from https://www.relishapp.com/rspec/rspec-rails/v/2-8/docs/view-specs/view-spec
  it "shows the logo" do
    render :template => "layouts/_footer.html.erb"
    rendered.should =~ "/images/mainlogo.png"
  end

describe "rendering views/layouts/_footer.html.erb as admin" do
  before do
    FactoryGirl.create(:admin_user)
  end 
  it "links to landing from the logo"
    render :template => "layouts/_footer.html.erb"
    rendered.should contain("link/to/admin/landing")
  end
end
#repeat landing tests for various user types

And then, in other pages, I can simply test for the presence of the footer itself using something like

  it should contain("footer")

My problem is, I can't even get off the ground to check to see if the image has been shown, much less if the image matches the right one in the assets directory. What should I be doing here?

The above code for testing the presence of the image (just the first describe/it block, not the stuff with the 'as admin' or the 'should contain') gives me the following warning and error:

DEPRECATION WARNING: Passing a template handler in the template name is deprecated.

TypeError:
   type mismatch: String given

The first is probably due to me using syntax I don't understand, but the second seems to suggest that the comparison is with strings rather than assets. What's the syntax to compare images? Is there one?

mmr
  • 14,781
  • 29
  • 95
  • 145
  • After some more searching, I found that this is a possible duplicate of [How do I find an image on a page with Cucumber / Capybara in Rails 3](http://stackoverflow.com/questions/4812606/how-do-i-find-an-image-on-a-page-with-cucumber-capybara-in-rails-3) – mmr Jan 31 '13 at 00:27
  • Does that test whether the image was actually *rendered* or just present in the html? (perhaps that is a silly question, but I mean to say is whether there is an image file at the image location) – Andy Hayden Jan 31 '13 at 00:50
  • I'd rather the first, but honestly, at this point, I'll take either. This kind of "Guess the magic word I'm thinking of! Go on, guess!" style of development is extremely frustrating. – mmr Jan 31 '13 at 00:55

1 Answers1

3

Your type mismatch error is because you're passing a string to =~ which is a regular expression matcher. You can either change the string to a regular expression, or use the include instead. I'd go with include just because it's more readable:

rendered.should include("/images/mainlogo.png")

To get rid of the deprecation warning, just remove the .erb from the template name:

render :template => "layouts/_footer.html"
Sam Peacey
  • 5,964
  • 1
  • 25
  • 27