test folder structureTest enviromentSimple test fileUse describe and should (or it)Run the testHow to use beforeSource
test
folder structure
$ ls -F test
Test enviroment
RAILS_ENV=test
Or modify it in
config/environments/test.rb
Simple test file
require "test_helper"
class ArticleTest < ActiveSupport::TestCase
test "the truth" do
assert true
end
end
Use describe
and should
(or it
)
describe "method name" do
should "do something expectedly or unexpectedly" do
# ...
end
end
Run the test
# test the entire file
$ bin/rails test test/models/article_test.rb
# test a specific test inside describe
$ bin/rails test test/models/article_test.rb:6
How to use before
before(:all)
runs the block one time before all of the examples are run.before(:each)
runs the block one time before each of your specs in the file:before(:each)
resets the instance variables in the before block every time an it block is run
before(:all)
sets the instance variables @admission, @project, @creative, @contest_entry
one time before all of the it
blocks are run.before(:all)
#before block is run
it { should belong_to(:owner).class_name('User') }
it { should belong_to(:project) }
it { should have_many(:entry_comments) }
it { should validate_presence_of(:owner) }
it { should validate_presence_of(:project) }
it { should validate_presence_of(:entry_no) }
it { should validate_presence_of(:title) }
before(:each)
# before block
it { should belong_to(:owner).class_name('User') }
# before block
it { should belong_to(:project) }
# before block
it { should have_many(:entry_comments) }
# before block
# before block
it { should validate_presence_of(:owner) }
# before block
it { should validate_presence_of(:project) }
# before block
it { should validate_presence_of(:entry_no) }
# before block
it { should validate_presence_of(:title) }