Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rake test not picking up capybara tests in minitest

I am setting up a basic template for having a capybara feature test in a rails application. I'm also using MiniTest instead of RSPEC.

Running Rake Test does not seem to be picking up my feature tests. I have one test in the file, running rake test does not change the number of assertions. Skipping the test does not show up either when I run rake test.

Here is a link to the repository: https://github.com/rrgayhart/rails_template

Here are the steps that I followed

  1. I added this to the Gemfile and ran bundle

    group :development, :test do
      gem 'capybara'
      gem 'capybara_minitest_spec'
      gem 'launchy'
    end
    
  2. I added this to the test_helper

    require 'capybara/rails'
    
  3. I created a folder test/features

  4. I created a file called drink_creation_test.rb

  5. Here is the code from that feature test file

    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
    

I think I am having an issue with not connecting something correctly. Please let me know if there is anything else I can provide which may help with diagnosing this issue.

like image 954
ccanduc Avatar asked Mar 22 '23 18:03

ccanduc


2 Answers

Multiple things going on here. First, the default rake test task won't pick up tests not in the default test directories. So you need to either move the test file or add a new rake task to test files in test/features.

Since you are using capybara_minitest_spec you need to include Capybara::DSL and Capybara::RSpecMatchers into your test. And because you aren't using ActiveSupport::TestCase or one of the other Rails test classes in this test, you may see inconsistencies in the database because this test is executing outside of the standard rails test transactions.

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

Or, you could use minitest-rails and minitest-rails-capybara to generate and run these tests.

$ rails generate mini_test:feature DrinkCreation
$ rake minitest:features
like image 151
blowmage Avatar answered Apr 01 '23 17:04

blowmage


I believe minitest has it's own gem for rails when using capybara: minitest-rails-capybara

Following the instructions over there might help, but I've never set up capybara with mini test before.

like image 45
Carl Avatar answered Apr 01 '23 17:04

Carl