Question

I have set up guard following Michael's RoR Tutorials and intentionally wrote a test (on contact page title) so it fails. But Guard/RSpec tells me it passed and I am confused what's going on. This is my static_pages_spec.rb file:

require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

    it "should have the content 'Welcome to the PWr App'" do
      visit '/static_pages/home'
      expect(page).to have_content('Welcome to the PWr App')
    end

    it "should have the title 'Home'" do
      visit '/static_pages/home'
      expect(page).to have_title("PWr | Home")
    end
  end

  describe "Help page" do

    it "should have the content 'Help'" do
      visit '/static_pages/help'
      expect(page).to have_content('Help')
    end

    it "should have title 'Help'" do
      visit '/static_pages/help'
      expect(page).to have_title("PWr | Help")
    end
  end

  describe "About page" do
    it "should have the content 'About me'" do
      visit '/static_pages/about'
      expect(page).to have_content('About Me')
    end

    it "should have title 'About Me'" do
      visit '/static_pages/about'
      expect(page).to have_title("PWr | About")
    end
  end

  describe "Contact page" do
    it "should have the content 'Contact'" do
      visit '/static_pages/contact'
      expect(page).to have_content('Contact')
    end

    it "should have title 'Contact'" do
      visit '/static_pages/contact' do
        expect(page).to have_title("FAIL")
      end
    end
  end
end

And this is my contact.html.erb:

<% provide(:title, 'Contact') %>
<h1>Contact</h1>
<p1>
        If you need to contact me just call the number below: </br>
        +48 737823884
</p>

And results from my terminal:

18:43:57 - INFO - Running: spec/requests/static_pages_spec.rb
........

Finished in 0.08689 seconds
8 examples, 0 failures


Randomized with seed 55897

[1] guard(main)> 

As you can see, in the spec file close to the end I have expect(page).to have_title("FAIL") and in the contact page html/erb I clearly have <% provide(:title, 'Contact') %> but the test passes. Why is this? What am I doing wrong?

Was it helpful?

Solution

The problem is that you are passing your expectation as a block to the visit method - ie notice the extra do-end. I do not believe that visit uses blocks, so basically that bit of code gets ignored.

it "should have title 'Contact'" do
  visit '/static_pages/contact' do
    expect(page).to have_title("FAIL")
  end
end

Your spec should behave as expected if you remove the block.

it "should have title 'Contact'" do
  visit '/static_pages/contact'
  expect(page).to have_title("FAIL")
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top