【问题标题】:Rake test not picking up capybara tests in minitest瑞克测试没有在 minitest 中进行水豚测试
【发布时间】:2013-11-05 22:42:38
【问题描述】:

我正在设置一个基本模板,用于在 rails 应用程序中进行 capybara 功能测试。我也在使用 MiniTest 而不是 RSPEC。

Running Rake Test 似乎没有进行我的功能测试。我在文件中有一个测试,运行 rake test 不会改变断言的数量。当我运行 rake 测试时,也不会出现跳过测试。

这里是存储库的链接:https://github.com/rrgayhart/rails_template

这是我遵循的步骤

  1. 我将它添加到 Gemfile 并运行 bundle

    group :development, :test do
      gem 'capybara'
      gem 'capybara_minitest_spec'
      gem 'launchy'
    end
    
  2. 我将此添加到 test_helper

    require 'capybara/rails'
    
  3. 我创建了一个文件夹 test/features

  4. 我创建了一个名为drink_creation_test.rb 的文件

  5. 这是该功能测试文件中的代码

    require 'test_helper'
    
    class DrinkCreationTest < MiniTest::Unit::TestCase
    
      def test_it_creates_an_drink_with_a_title_and_body
          visit drinks_path
          click_on 'new-drink'
          fill_in 'name', :with => "PBR"
          fill_in 'description', :with => "This is a great beer."
          fill_in 'price', :with => 7.99
          fill_in 'category_id', :with => 1
          click_on 'save-drink'
          within('#title') do
            assert page.has_content?("PBR")
          end
          within('#description') do
            assert page.has_content?("td", text: "This is a great beer")
          end
      end
    
    end
    

我认为我遇到了无法正确连接某些东西的问题。 如果我可以提供任何其他有助于诊断此问题的信息,请告诉我。

【问题讨论】:

    标签: ruby-on-rails capybara minitest


    【解决方案1】:

    这里发生了很多事情。首先,默认的rake test 任务不会选择不在默认测试目录中的测试。因此,您需要移动测试文件或添加新的 rake 任务以测试 test/features 中的文件。

    由于您使用的是capybara_minitest_spec,因此您需要在测试中包含Capybara::DSLCapybara::RSpecMatchers。由于您在此测试中没有使用ActiveSupport::TestCase 或其他 Rails 测试类之一,因此您可能会在数据库中看到不一致的情况,因为此测试在标准 Rails 测试事务之外执行。

    require 'test_helper'
    
    class DrinkCreationTest < MiniTest::Unit::TestCase
      include Capybara::DSL
      include Capybara::RSpecMatchers
    
      def test_it_creates_an_drink_with_a_title_and_body
          visit drinks_path
          click_on 'new-drink'
          fill_in 'name', :with => "PBR"
          fill_in 'description', :with => "This is a great beer."
          fill_in 'price', :with => 7.99
          fill_in 'category_id', :with => 1
          click_on 'save-drink'
          within('#title') do
            assert page.has_content?("PBR")
          end
          within('#description') do
            assert page.has_content?("td", text: "This is a great beer")
          end
      end
    
    end
    

    或者,您可以使用minitest-railsminitest-rails-capybara 生成运行这些测试。

    $ rails generate mini_test:feature DrinkCreation
    $ rake minitest:features
    

    【讨论】:

    • 太棒了!这是一个完美的答案——如果我们将它们移动到集成中(这与您的 rake 任务评论有关),我们能够获得测试——但最终转移到移动 minitest 特定的 gem。再次感谢您!
    【解决方案2】:

    我相信 minitest 在使用 capybara 时拥有自己的 Rails 宝石:minitest-rails-capybara

    按照那边的说明可能会有所帮助,但我之前从未设置过水豚迷你测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      相关资源
      最近更新 更多