0

我已经按照迈克尔的 RoR 教程设置了警卫,并故意编写了一个测试(在联系页面标题上),所以它失败了。但是 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)> 

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

4

1 回答 1

3

问题是您将期望作为一个块传递给 visit 方法 - 即注意额外的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
于 2013-11-06T18:42:25.940 回答