3

我无法使用 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)>'
4

2 回答 2

3

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

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

它应该在哪里

page.should have_title("#{site_title} | Home")
于 2013-05-23T04:08:17.983 回答
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
于 2013-05-22T23:57:15.133 回答