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
I added this to the Gemfile and ran bundle
group :development, :test do
gem 'capybara'
gem 'capybara_minitest_spec'
gem 'launchy'
end
I added this to the test_helper
require 'capybara/rails'
I created a folder test/features
I created a file called drink_creation_test.rb
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With