2011-05-22 155 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')並彈出這些行來解決問題。