4

Rails アプリケーションでカピバラの機能テストを行うための基本的なテンプレートをセットアップしています。RSPEC の代わりに MiniTest も使用しています。

Rake Test を実行しても機能テストが反映されていないようです。ファイルに 1 つのテストがあり、rake テストを実行してもアサーションの数は変わりません。rake test を実行しても、テストをスキップしても表示されません。

リポジトリへのリンクは次のとおりです: https://github.com/rrgayhart/rails_template

これが私が従った手順です

  1. これをGemfileに追加してバンドルを実行しました

    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
    

何かを正しく接続していないという問題があると思います。この問題の診断に役立つ情報が他にあればお知らせください。

4

2 に答える 2

5

ここで複数のことが起こっています。まず、デフォルトrake testタスクは、デフォルト テスト ディレクトリにないテストを取得しません。そのため、テスト ファイルを移動するか、新しい rake タスクを追加して でファイルをテストする必要がありますtest/features

capybara_minitest_specを使用しているため、テストにCapybara::DSLとを含める必要がありCapybara::RSpecMatchersます。また、このテストでは または他の Rails テスト クラスのいずれかを使用していないActiveSupport::TestCaseため、このテストは標準の 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
于 2013-11-07T17:00:22.477 に答える
2

カピバラを使用する場合、minitest にはレール用の独自の gem があると思います: minitest-rails-capybara

そこの指示に従うと役立つかもしれませんが、ミニテストでカピバラをセットアップしたことはありません。

于 2013-11-05T22:51:39.737 に答える