2011-05-22 102 views
7

希望有人可能会看到我忽略了什么...从Webrat迁移到水豚...失败

我试图让水豚在一个现有的小应用程序中工作......并且我没有运气好的话。

的Gemfile:

group :development, :test do 
    gem 'rspec-rails' 
    # gem 'webrat' 
    gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git' 
    end 
    ... 

类似的规格在两个地方是失败的原因不尽相同。不知道为什么?

规格/控制器/ pages_controller_spec.rb:

require 'spec_helper' 

describe PagesController do 

    describe "GET 'about'" do 
    it "should be successful" do 
     # get 'about'       #worked w/ webrat 
     # response.should be_success   #worked w/ webrat 
     visit pages_about_path 
     # page.should have_content('About Us') 
     page.html.should match(/About/i) 
    end 

    it "should have title" do 
     # get 'about'       #webrat 
     # response.should have_selector("title", :content => "About Us") #webrat 
     visit pages_about_path     
     page.should have_selector("title")  
    end 
    end 
end 

失败: (可能在一些通用的页被拉动为文档类型在浏览器是"<!DOCTYPE html>"

1) PagesController GET 'about' should be successful 
    Failure/Error: page.html.should match(/About/i) 
     expected "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n\n" to match /About/i 
    # ./spec/controllers/pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>' 

    2) PagesController GET 'about' should have the right title 
    Failure/Error: page.should have_selector("title") 
     expected css "title" to return something 
    # ./spec/controllers/pages_controller_spec.rb:20:in `block (3 levels) in <top (required)>' 

spec/views/pages/about.html.haml_spec.rb:

require 'spec_helper' 

describe "pages/about.html.haml" do 
    it "renders attributes in <p>" do 
    # render #webrat 
    # rendered.should match(/About/) #webrat 
    visit pages_about_path 
    page.should have_content("About Us") 
    end 

    it "should have the right heading" do 
    # render #webrat 
    # rendered.should have_selector("h2", :content => "About Us") #webrat 
    visit pages_about_path 
    page.should have_selector("h2") 
    end 
end 

失败:

1) pages/about.html.haml renders attributes in <p> 
    Failure/Error: visit pages_about_path 
    NoMethodError: 
     undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000101dc2970> 
    # ./spec/views/pages/about.html.haml_spec.rb:8:in `block (2 levels) in <top (required)>' 

    2) pages/about.html.haml should have the right heading 
    Failure/Error: visit pages_about_path 
    NoMethodError: 
     undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x000001034b1d98> 
    # ./spec/views/pages/about.html.haml_spec.rb:16:in `block (2 levels) in <top (required)>' 
+0

请标记nigelr(或任何你认为值得的人)作为其他可能有这个问题和普遍礼貌的人接受的答案。 – KobeJohn 2012-01-13 05:13:07

回答

14

刚刚有同样的问题,结果发现水豚需要:

response.body.should have_selector("title") 

webrat不需要。体

也,确保您渲染视图例如:。

describe PagesController do 
    render_views 
+0

这是答案。另外,如果你来自railstutorial.org,我相信你也需要改变:content =>“whatever”to:title =>“whatever”。否则,无论内容如何,​​它都会通过。 – KobeJohn 2012-01-13 05:12:15

+0

我认为yakiimo意味着'have_selector(“title”,:content =>“Home”)'成为'have_selector(“title”,:text =>“Home”)''。我还发现'have_selector(“a”,:href =>“/ users?page = 2”,:content =>“Next”)'has_link(“Next”,:href =>“/ users?页= 2" )'。我在这篇博客文章中发现了前后的例子:[从Webrat迁移到Capybara](http://zadasnotes.blogspot.com/2011/08/migrating-from-webrat-to-capybara.html )。 – 2012-04-04 20:54:44

0

这不会解决所有的问题,而是组成:

page.html.should match(/About/i) 

尝试:

page.should match(/About/i) 

(你威力甚至不需要页面)。

此外,请检查您是否将Capybara设置为在env.rb中使用CSS查询(它默认为XPath)。

+0

删除'html'。没有工作。此外,水豚的读到我说:“水豚不会猜测你要给它什么样的选择,并且**会默认使用CSS **如果你想使用XPath,你需要做...“看到错误”**未定义的方法'访问'**“让我觉得水豚没有正确安装或引用......但不知道如何确认!?! – Meltemi 2011-05-24 19:19:14

+0

嗯,env.rb黄瓜生成说:“水豚默认为XPath选择器,而不是Webrat的默认CSS3”。但是,这听起来好像不是拾起水豚。你尝试过Bundler运行吗?捆绑执行黄瓜 – 2011-05-24 19:52:01

+0

不使用黄瓜......不是真的想......可能是这个问题? – Meltemi 2011-05-24 20:03:21

1

你应该在你的测试文件豚DSL:

require 'spec_helper' 

    describe PagesController do 

    include Capybara::DSL 

     describe "GET 'about'" do 
     it "should be successful" do 
     ... 
0

确保您的spec_helper.rb文件的顶部有以下几点:

ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 
require 'rspec/autorun' 
require 'capybara/rspec' 

我也有类似的问题,因为你(即undefined method 'visit')并弹出这些行来解决问题。