2011-03-22 78 views
0

我有一堆用rspec編寫的組合控制器/視圖測試。我添加了水豚寶石,並寫了一些整合測試,通過罰款。唯一的問題是,現在在我所有的控制器的測試,在那裏我有安裝Capybara後控制器損壞?

response.should have_selector(「一些選擇」)

rspec的給錯誤,如:

NoMethodError: 
     undefined method `has_selector?' for #<ActionController::TestResponse:0xa03e7ec> 

當我運行控制器試驗。我猜測Capybara正在我的控制器測試中使用,並且已經覆蓋了一些Rspec方法。我怎樣才能解決這個問題?

# gemfile.rb 
group :test do 
    gem 'rspec' 
    gem "capybara" 
    gem "launchy" 
    gem 'factory_girl_rails', '1.0' 
end 

# spec_helper.rb 
RSpec.configure do |config| 
    config.include IntegrationSpecHelper, :type => :request 
end 

下面是一個失敗的測試的例子:

# spec/controllers/books_controller_spec.rb 
require 'spec_helper' 

describe BooksController do 
    render_views 

    it "should have the right page title" do 
    get :show, :id => @book.ean 
    response.should have_selector("title", :content => "Lexicase | " + @book.title) 
    end 
end 

和它相關的錯誤:

1) BooksController GET 'show' should have the right page title 
    Failure/Error: response.should have_selector("title", :content => "Lexicase | " + @book.title) 
    NoMethodError: 
     undefined method `has_selector?' for #<ActionController::TestResponse:0xa8488c0> 
    # ./spec/controllers/books_controller_spec.rb:23:in `block (3 levels) in <top (required)>' 
+0

您可以添加失敗的測試嗎? – shingara 2011-03-22 08:46:06

+0

更新了原來的帖子。 – 2011-03-22 08:57:51

回答

2

你可能使用Webrat較早,並has_selector?是一個Webrat匹配器。 Capybaras沒有has_selector匹配器,它有一個稱爲has_css的匹配器。您可能希望將「has_selector」替換爲「has_css」。

+1

無論如何,你對Webrat是正確的。據我所知,有一個has_selector?方法在Capybara(http://rubydoc.info/github/jnicklas/capybara/master/Capybara/RSpecMatchers),但它在頁面方法而不是請求方法。另一件事是,我不認爲我可以像webling一樣使用「get:show」。我必須使用訪問(book_path(@ book.ean))。然而,訪問只能通過設計發出請求,這使得它不適合進行控制器單元測試。我在這裏錯過了一個更大的問題。 – 2011-03-22 09:30:27

+1

大聲笑,事實證明,水豚和Webrat並不相互排斥,正如我最初設想的那樣。問題解決了! – 2011-03-22 09:56:00

+3

我希望部落格博主停止給Capybara提供Webrat的替代替代品,或者至少通過對您的代碼進行更改來限制聲明。我必須做很多小的改動才能讓我的代碼在切換後正常工作。 – 2011-03-23 17:21:20

1

水豚助手只能在請求規格內工作。創建一個新的要求規範,或傳遞:類型=>:要求在描述塊的一部分,就像這樣:

describe "test for the testing test", :type => :request do 
    it "should work with capybara" do 
    visit root_path 
    click_link "Home" 
    page.should WHATEVA 
    end 
end 

我意識到這個問題被問得很久以前,但我想我無論如何都會分享。 GLHF