2012-02-20 63 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

相关问题