Rails Test Prescriptions Chapter 2
What's a test?
What's a test? It “should do something” do #Logic end
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" do let(:me) { User.new(name: "Noel", Password: “foobar”) } let(:you) { Factory(:user) } it "should let users be friends" do me.add_friend(you) you.should have(1).friend end 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 models spec/models/user_spec.rb
How is Rspec different from Test::Unit “ Functional tests” usually  test controllers spec/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

Rtt preso

  • 1.
  • 2.
  • 3.
    What's a test?It “should do something” do #Logic end
  • 4.
    What's a test? it { should validate_uniqueness_of(:username) }
  • 5.
    What goes ina test?
  • 6.
    What goes ina test? Set up your data
  • 7.
    What goes ina test? Factories
  • 8.
    What goes ina 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
  • 9.
    What goes ina test? Perform the action
  • 10.
    What goes ina test? Perform the assertion
  • 11.
    What goes ina test? Tear Down
  • 12.
    What goes ina test? Tear Down (Rarely needed)
  • 13.
    Por ejemplo describe"user behavior" do let(:me) { User.new(name: "Noel", Password: “foobar”) } let(:you) { Factory(:user) } it "should let users be friends" do me.add_friend(you) you.should have(1).friend end end
  • 14.
    What can youtest in Rails? Models
  • 15.
    What can youtest in Rails? Controllers
  • 16.
    What can youtest in Rails? Views
  • 17.
    What can youtest in Rails? Views (but view tests are brittle)
  • 18.
    How is Rspecdifferent from Test::Unit “ Unit tests” usually test models spec/models/user_spec.rb
  • 19.
    How is Rspecdifferent from Test::Unit “ Functional tests” usually test controllers spec/controllers/users_controller_spec.rb
  • 20.
    How is Rspecdifferent from Test::Unit “ Integration tests” usually test multiple controllers
  • 21.
    What Happens whenRails Tests Run?
  • 22.
    What Happens whenRails Tests Run? Reset fixture data.
  • 23.
    What Happens whenRails Tests Run? Run any defined “let” “ setup” or “before” blocks
  • 24.
    What Happens whenRails Tests Run? Run the actual test method.
  • 25.
    What Happens whenRails Tests Run? Run all teardown blocks
  • 26.
    Running your tests$ bundle exec guard start
  • 27.