2012-02-20 54 views
2

So Rails爲test/functional目錄中的控制器生成一些功能測試。這些測試從ActionController::TestCase延伸。在Rails 3.x功能測試中使用Capybara的正確方法是什麼?

但在水豚的網站,他們只展示瞭如何通過猴子修補ActionDispatch::IntegrationTest準備集成測試:

DatabaseCleaner.strategy = :truncation 

class ActionDispatch::IntegrationTest 
    # Make the Capybara DSL available in all integration tests 
    include Capybara::DSL 

    # Stop ActiveRecord from wrapping tests in transactions 
    self.use_transactional_fixtures = false 

    teardown do 
    DatabaseCleaner.clean # Truncate the database 
    Capybara.reset_sessions! # Forget the (simulated) browser state 
    Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver 
    end 
end 

但是他們不提如何設置水豚與功能測試使用。什麼是正確的方法來做到這一點?

回答

0

水豚提供了一些非常好的語法,也可以在功能測試中使用。你只需要包裝頁面函數來返回一個Capybara :: Node :: Simple wrapped @ response.body。

class Test::Unit::TestCase 
    def page 
    Capybara::Node::Simple.new(@response.body) 
    end 
end 

從水豚的RDoc:

A {水豚::節點::簡單}是{水豚::節點::基}僅包括{水豚::節點的簡化版本:: Finders}和{Capybara :: Node :: Matchers},不包括{Capybara :: Node :: Actions}。

莫信息:

http://robots.thoughtbot.com/post/8087279685/use-capybara-on-any-html-fragment-or-page

相關問題