0

我正在关注 Michael Hartl 的 Ruby on rails 教程。

使用 rspec 重构一个简单的测试失败了,我将首先粘贴有效的内容,然后粘贴有效的内容....

require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

    it "should have the h1 'Sample App'" do
      visit '/static_pages/home'
      page.should have_selector('h1', :text => 'Sample App')
    end

    it "should have the title 'Home'" do
      visit '/static_pages/home'
      page.should have_selector('title',
                        :text => "Ruby on Rails Tutorial Sample App | Home")
    end
   end

    describe "Help page" do

    it "should have the h1 'Help'" do
      visit '/static_pages/help'
      page.should have_selector('h1', :text => 'Help')
    end

    it "should have the title 'Help'" do
      visit '/static_pages/help'
      page.should have_selector('title',
                        :text => "Ruby on Rails Tutorial Sample App | Help")
    end
  end
end

现在我将粘贴 rspec 中失败的内容

require 'spec_helper'

describe "Static pages" do

  let(:base_title) { "Ruby on Rails Tutorial Sample App |" }

  describe "Home page" do

    it "should have the h1 'Sample App'" do
      visit '/static_pages/home'
      page.should have_selector('h1', :text => 'Sample App')
    end

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



  describe "Help page" do

    it "should have the h1 'Help'" do
      visit '/static_pages/help'
      page.should have_selector('h1', :text => 'Help')
    end

    it "should have the title 'Help'" do
      visit '/static_pages/help'
      page.should have_selector('title', :text => "#{base_title}  Help")
    end
  end 
end

我也是新手,所以我想问一下可能需要更多信息......变化显然是

  let(:base_title) { "Ruby on Rails Tutorial Sample App |" }

谢谢你的帮助 !

失败的错误是......

失败:

1)静态页面主页应该有标题'Home'失败/错误:page.should have_selector('title',:text =>“#base_title} Home”)预期的css“title”和文本“#base_title} Home”返回一些东西 # ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in '

2)静态页面帮助页面应该有标题'Help'失败/错误:page.should have_selector('title',:text =>“#{base_title}Help”)预期的css“title”和文本“Ruby on Rails Tutorial示例应用程序 | 帮助”返回一些东西 # ./spec/requests/static_pages_spec.rb:29:in `block (3 levels) in '

4

3 回答 3

1

自从提出这个问题以来已经有一段时间了。如果它仍然相关:

后面有两个空格#{base_title}

删除一个空格,它应该可以工作。

于 2012-12-04T21:06:41.977 回答
0

看起来您{在第一个示例中错过了 a:

 "title" with text "#base_title} Home"

这个字符串只是没有插值,与let

如果您的代码和测试正常工作,则不应发生其他错误。

于 2012-07-13T14:26:53.543 回答
0

领先于改变 Capybara 和 Webrat 的语法。这应该适用于您的示例:

 page.should have_selector('h1', text: "#{base_title}  Help")

或者你可以用这样的类替换它:

page.should have_selector("h1[class='title']", text: "#{base_title} Help)
于 2012-07-13T23:50:47.817 回答