If you can't read please download the document
Upload
fdschoeneman
View
1.059
Download
0
Embed Size (px)
Citation preview
Rails Test Prescriptions
Chapter 2
What's a test?
What's a test?
It should do something do#Logicend
What's a test?
it { should validate_uniqueness_of(:username) }
What goes in a test?
What goes in a test?
Set up your data
What goes in a test?
Factories
What goes in a test?
# In spec/factories/users.rb
FactoryGirl.define do
factory :user do sequence(:email) { |n| "user#{n}@generalthings.com" } sequence(:username) { |n| "user#{n}name" } first_name Faker::Name.first_name last_name Faker::Name.last_name phone_number Faker::PhoneNumber.phone_number title Faker::Name.prefix password "password" bio Faker::Lorem.sentences end
end
What goes in a test?
Perform the action
What goes in a test?
Perform the assertion
What goes in a test?
Tear Down
What goes in a test?
Tear Down(Rarely needed)
Por ejemplo
describe "user behavior" dolet(:me) { User.new(name: "Noel",Password: foobar) }let(:you) { Factory(:user) }
it "should let users be friends" dome.add_friend(you)you.should have(1).friendend
end
What can you test in Rails?
Models
What can you test in Rails?
Controllers
What can you test in Rails?
Views
What can you test in Rails?
Views(but view tests are brittle)
How is Rspec different from Test::Unit
Unit tests usually test modelsspec/models/user_spec.rb
How is Rspec different from Test::Unit
Functional tests usually test controllersspec/controllers/users_controller_spec.rb
How is Rspec different from Test::Unit
Integration tests usually test multiple controllers
What Happens when Rails Tests Run?
What Happens when Rails Tests Run?
Reset fixture data.
What Happens when Rails Tests Run?
Run any defined let setup or before blocks
What Happens when Rails Tests Run?
Run the actual test method.
What Happens when Rails Tests Run?
Run all teardown blocks
Running your tests
$ bundle exec guard start
Running your tests
$ begs