Rails testing has serveral options: MiniTest, RSpec, Test::Unit, Cucumber. Test Frameworks make it easier to write and maintain tests. Rake provides rake test
and other variations for automated testing.
Example Gemfile for test environment:
group :test do
gem 'minitest-reporters', '1.0.5'
gem 'mini_backtrace', '0.1.3'
gem 'guard-minitest', '2.3.1'
end
Testing Framework in Ruby's vast Standard Library. Faster than RSpec by running tests in parallel. Written more like Ruby code.
Used for unit testing in rails. ActiveRecord::TestCase inherits MiniTest::Test. Automatically generated in Rails for each model.
class TruthTest < MiniTest::Unit::TestCase
def test_truth
assert true
end
end
Checks truth by each assert
statement.
In MiniTest this is done with assertions that will test the object by assert something about it. Take this example of a Post model test.
post = Post.create(title: 'Post title')
# post should be valid
assert post.valid?
Here we are creating a post object and checking if it is valid to ensure that it got created.
One of the built-in ruby testing frameworks. Run test cases in Rails with bin/rspec
(reqiures being initialized).
Good for running individual tests
require 'rspec'
describe "Truth" do
it "should be true" do
true.should == true
end
end
Built-in to ActiveRecord, Fixtures let you test the ActiveRecords themselves (testing on the database) by populating the testing database with sample data (in yaml format).
before_create
to ensure that your data is being generated as you expected.Uses ActionController::TestCase < ActiveSupport::TestCase < MiniTest::Test
. Also MiniTest! Introduces more assert
s like assert_response
for http responses.
Can be used to directly test http requests using the http VERBS:
test "should create article" do
assert_difference('Article.count') do
post :create, article: {title: 'Some title'}
end
assert_redirected_to article_path(assigns(:article))
end
Views can be tested using assert_select
to verify HTML content on page:
assert_select 'title', "Welcome to Rails Testing Guide"
assert_select(selector, [equality], [message])
ensures that the equality condition is met on the selected elements through the selector. The selector may be a CSS selector expression (String) or an expression with substitution values.
assert_select(element, selector, [equality], [message])
ensures that the equality condition is met on all the selected elements through the selector starting from the element (instance of Nokogiri::XML::Node or Nokogiri::XML::NodeSet) and its descendants.
Tests interaction among controllers. Test work flow for the application. In test/integration
folder.
From ActionDispatch::IntegrationTest < ActiveSupport::TestCase
.