Rspec让变量不能与have_title一起使用

时间:2013-05-22 22:03:45

标签: ruby-on-rails rspec capybara

我无法使用Capybara 2.1.0进行我的Rspec测试之一

这有效:

require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

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

这不是

require 'spec_helper'

describe "Static pages" do

  let(:site_title) {"MyApp"}

  describe "Home page" do

      it "should have the title 'Home'" do
      visit '/static_pages/home'
      page.should have_title("#{site_title} | Home")
    end
  end
end

有什么想法?我已经做了很长一段时间了。这是我收到的错误消息。

1) Static pages Home page should haven the title
 Failure/Error: page.should have_title("#{site_title} | Home")
   expected #has_title?("MyApp | Home") to return true, got false
 # ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>'

2 个答案:

答案 0 :(得分:3)

您似乎缺少#{site_title}的大括号,或者您需要为字符串使用双引号来允许插值变量。你有:

page.should have_title('#site_title | Home')

应该在哪里

page.should have_title("#{site_title} | Home")

答案 1 :(得分:0)

试试这个:

require 'spec_helper'

describe "Static pages" do

  let(:site_title) {"MyApp"}

  describe "Home page" do

      it "should have the title 'Home'" do
      visit '/static_pages/home'
      page.should have_selector('title', text: "#{site_title} | Home")
    end
  end
end