$ rake db:test:prepare
Ensures that the db in development environment is propagated to the test environment.
To have pending RSpec tests, you do the following:
require ‘spec_helper’
describe User do
pending “add some examples to (or delete) #{__FILE__}”
end
…
If you want to prepare data prior to testing:
before(:each) do
@attr = { :name => “Example User”, :email => “user@example.com” }
end
When checking for validity of records, you can do it as:
no_name_user.should_not be_valid
or
no_name_user.valid?.should_not == true
With the code below, we are testing that a certain object has a certain attribute (with responds_to):
it “should have an encrypted password attribute” do
@user.should respond_to(:encrypted_password)
end
Adding factories to create testing objects can save a lot of time, there is a gem that can be added to your Gemfile, in the test environment to do this:
group :test do
. . .
gem ‘factory_girl_rails’, ‘1.0’
end
Once the gem is install (bundle install), you can create the following file:
spec/factories.rb
And here’s a sample content:
# By using the symbol ‘:user’, we get Factory Girl to simulate the User model.
Factory.define :user do |user|
user.name “Michael Hartl”
user.email “mhartl@example.com”
user.password “foobar”
user.password_confirmation “foobar”
end
So now, factory objects of the order :user can be created for testing purposes as follows:
@user = Factory(:user)
Here’s an example of a Factory that contains associations between two different objects:
# By using the symbol ‘:user’, we get Factory Girl to simulate the User model.
Factory.define :user do |user|
user.name “Michael Hartl”
user.email “mhartl@example.com”
user.password “foobar”
user.password_confirmation “foobar”
end
Factory.sequence :email do |n|
“person-#{n}@example.com”
end
Factory.define :micropost do |micropost|
micropost.content “Foo bar”
micropost.association :user
end
If you want to clear the test database of any user data still hanging there, use:
$ rake db:reset
Rails 4 update
Example of a model test (creating an article). The test resides in /test/models/
require 'test_helper' class ArticleTest < ActiveSupport::TestCase test "should create article" do article = Article.new article.user = users(:eugene) article.title = "Test article" article.body = "Test body" assert article.save end end
Some of the most used available assertions:
assert(boolean, message=nil) assert_block(message="assert_block failed.") do ... end assert_equal(expected, actual, message=nil) assert_in_delta(expected_float, actual_float, delta, message="") assert_instance_of(klass, object, message="") assert_kind_of(klass, object, message="") assert_match(pattern, string, message="") assert_nil(object, message="") assert_no_match(regexp, string, message="") assert_not_equal(expected, actual, message="") assert_not_nil(object, message="") assert_not_same(expected, actual, message="") assert_nothing_raised(*args) do ... end assert_nothing_thrown(message="") do ... end assert_operator(object1, operator, object2, message="") assert_raise(expected_exception_klass, message="") do ... end assert_respond_to(object, method, message="") assert_same(expected, actual, message="") assert_send(send_array, message="") assert_throws(expected_symbol, message="") do ... end
To run it:
$ rake test:models
In order to generate a template for integration testing:
$rails generate test_unit:integration UserStories
To run that particular test:
$ruby -Itest test/integration/user_stories_test.rb
And, to run the full test suite:
$rake
or
$bundle exec rspec [name of the spec you want to run, optional]