rspec测试中的奇怪行为

时间:2012-06-19 23:23:06

标签: ruby-on-rails rspec

我正在用我的RoR应用程序编写测试。我的static_pages_spec.rb

    # encoding: utf-8

require 'spec_helper'

describe "Static pages" do

  subject { page }

  describe "Home page" do
    before { visit root_path }

    it { should have_selector('h1',    text: 'Giripedia') }
    it { should have_selector('title', text: full_title('')) }
    it { should_not have_selector 'title', text: '| Home' }
  end

  describe "Help page" do
    before { visit help_path }

    it { should have_selector('h1',    text: 'Yardım') }
    it { should have_selector('title', text: full_title('Help')) }
  end

  describe "About page" do
    before { visit about_path }

    it { should have_selector('h1',    text: 'Hakkımızda') }
    it { should have_selector('title', text: full_title('About Us')) }
  end

  describe "Contact page" do
    before { visit contact_path }

    it { should have_selector('h1',    text: 'İletişim') }
    it { should have_selector('title', text: full_title('Contact')) }
  end
end

并在我的spec / support / utilites.rb文件中

def full_title(page_title)
  base_title = "Ruby on Rails Tutorial Sample App"
  if page_title.empty?
    base_title
  else
    "#{base_title} | #{page_title}"
  end
end

如果我改变base_title =“Giripedia论坛”。测试失败并给我错误:

1) Static pages Home page 
     Failure/Error: it { should have_selector('title', text: full_title('')) }
       expected css "title" with text "Giripedia forum" to return something
     # ./spec/requests/static_pages_spec.rb:13:in `block (3 levels) in <top (required)>'

  2) Static pages Help page 
     Failure/Error: it { should have_selector('title', text: full_title('Help')) }
       expected css "title" with text "Giripedia forum | Help" to return something
     # ./spec/requests/static_pages_spec.rb:21:in `block (3 levels) in <top (required)>'

  3) Static pages About page 
     Failure/Error: it { should have_selector('title', text: full_title('About Us')) }
       expected css "title" with text "Giripedia forum | About Us" to return something
     # ./spec/requests/static_pages_spec.rb:28:in `block (3 levels) in <top (required)>'

  4) Static pages Contact page 
     Failure/Error: it { should have_selector('title', text: full_title('Contact')) }
       expected css "title" with text "Giripedia forum | Contact" to return something
     # ./spec/requests/static_pages_spec.rb:35:in `block (3 levels) in <top (required)>'

为什么base_title中的更改在测试中开始失败?

1 个答案:

答案 0 :(得分:1)

我可以在这里找到我的错误。可能这会帮助别人。

我只更改了spec文件中的base_title。但是我应该更改属于被测试的app / helper的base_title。

我知道,我应该好好阅读测试日志。