maxenglander
maxenglander

Reputation: 4041

rspec does not fail when specifying non-existent attribute for a have_selector

I am starting to use RSpec2 to test my Rails 3 application. Here is a simple RSpec I have so far:

require 'rspec_helper'

describe "the sign in process" do 
  it "should have a logo" do
    visit "/"
    page.should have_selector("#login-logo") do |login_logo|
      login_logo.should have_selector("img", :src => "no such image")
    end 
  end

  it "should have an email address label" do
    visit "/"
    page.should have_selector("label", :for => "no such input") do |label|
      label.should has_content("Email Address")
    end
  end                                                                                                                                                                                                                                                                                                                              
end   

On the page in question ("/"), there is no img element with a src of "no such image"; neither is there a label tag with a for attribute of "no such input". Yet, my RSpec test passes when I run rspec /path/to/my_test.rb.

I want to force RSpec to fail when there is no element with a given attribute on a page.

Upvotes: 1

Views: 427

Answers (1)

Tom Hert
Tom Hert

Reputation: 947

I just solved the same issue, the

login_logo.should have_selector("img", :src => "no such image")

is Webrat syntax and if you're using Capybara then you should have syntax like this:

it "renders the form to create a post" do
  render
  rendered.find('form').tap do |form|
    form.should have_selector("input[type=submit][value='Save']")
  end
end

Here is the link, where is it more discribed

Upvotes: 1

Related Questions