Guard通过我的RSpec测试

时间:2013-11-06 17:50:56

标签: ruby-on-rails ruby rspec tdd guard

我在Michael's RoR Tutorials之后设置了警卫,故意写了一个测试(在联系页面标题上),所以它失败了。但Guard / RSpec告诉我它通过了,我很困惑发生了什么。这是我的static_pages_spec.rb文件:

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

这是我的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>

来自我的终端的结果:

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)> 

正如您所看到的,在接近结尾的spec文件中我有expect(page).to have_title("FAIL"),在联系页面html / erb中我明显有<% provide(:title, 'Contact') %>但是测试通过了。为什么是这样?我究竟做错了什么?

1 个答案:

答案 0 :(得分:3)

问题在于您将期望作为块传递给访问方法 - 即注意额外的do-end。我不相信visit使用块,所以基本上会忽略这些代码。

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

如果删除块,您的规范应该按预期运行。

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