This is an example a simple integration test in rails using minitest and capybara. It has three parts:
- first uses simple minitest syntax
- second uses minitest-spec-rails
- third uses minitest-spec-rails plus capybara_minitest_spec
rake
runs the default rake task which is test - runs all test cases. You can also run specific groups with
rake test:integration
and run single test files with
rake test TEST=test/integration/first_items_integration_test.rb
Minitest is a testing framework for ruby. It is the default test framework for Rails 4 and substitutes Unit::Test which was the default in Rails 3.2
Here's a short intro to using minitest with Rails 3.2
Note that you won't need any of the setup steps in rails 4 as minitest is already the default there.
It also replaces rspec, which was often used as an alternative to unit::test in rails 3.2.
Capybara is a library that enables you to automate website testing: Going to a page, clicking on links and buttons, checking if content is there.
You should have at least one integration test per story.
see the integration tests in
test/integration/
for examples. The rest of the testcases have been generated by the scaffold generator.
If you implement behaviour in the model, write a model unit test for it, I've included a very simple example in
test/models/item_test.rb
If you get stuck with your tests, it is often helpful to have a look at the generated page. To do that, include the launchy gem in your Gemfile and call
save_and_open_page
from within your test case.
Putting somethin like
assert_equal "xx", page.class
in one of your tests let's you find out the class of page, which is Capybara::Session.