# Errores / Excepciones
expect { ... }.to raise_error
expect { ... }.to raise_error(ErrorClass)
expect { ... }.to raise_error("message")
expect { ... }.to raise_error(ErrorClass, "message")
# Verdad / Falsedad
expect(actual).to be_truthy # passes if actual is truthy (not nil or false)
expect(actual).to be true# passes if actual == true
expect(actual).to be_nil # passes if actual is nil
expect(actual).to_not be_nil # passes if actual is not nil# Predicate matchers
expect(actual).to be_xxx # passes if actual.xxx?
expect(actual).to have_xxx(:arg) # passes if actual.has_xxx?(:arg)
Red, Green, Refactor...
Bloques before/after
Bloques let/let!
Context
before block
RSpec.describe 'Hello world'do
before do
@hw = HelloWorld.new
end
it "has a default greeting"do
expect(@hw.say_hi).to eq "Hello World"end
it "can receive a param"do
expect(@hw.say_hi "there!").to eq "Hello There!"endend
before block
before(:each)
before(:all)
after(:each)
after(:all)
let
RSpec.describe 'Hello world'do
let(:hw) { HelloWorld.new }
it "has a default greeting"do
expect(hw.say_hi).to eq "Hello World"end
it "can receive a param"do
expect(hw.say_hi "there!").to eq "Hello There!"endend
let + context
RSpec.describe 'Let inside Context'do
context "as a User"do
let (:user) { User.new }
# ...end
context "as an Admin"do
let (:user) { Admin.new }
# ...endend
let
lazy evaluation
memoised on first use
reseted between examples
let!
siempre se inicializa
Mock (double)
require"rspec/mocks/standalone"
obj = double(:book) # => #<Double "book">
obj = double(:book, pages:250)
obj.pages # => 250classUserattr_accessor:usernameend# `instance_double` valida contra objetos reales
user = instance_double("User", :admin, username:'admin') # OK
user = instance_double("User", :admin, email:'admin@example.com') # Error
Method Stubs
allow(book).to receive(:title) { "The RSpec Book" }
allow(book).to receive(:title).and_return("The RSpec Book")
allow(book).to receive_messages(
title:"The RSpec Book",
subtitle:"Behaviour-Driven Development with RSpec, Cucumber, and Friends")
Message Expectations
Similares a un method stub, pero el test espera que el método sea llamado. (En caso contrario, falla)
# Stub
person = double("person")
allow(Person).to receive(:find) { person }
# Expectation
person = double("person")
expect(Person).to receive(:find) { person }
# This is a YAML commentdavid: name:DavidHeinemeierHansson birthday:1979-10-15 profession:Systemsdevelopmentsteve: name:SteveRossKellock birthday:1974-09-27 profession:guywithkeyboard
Fixtures associations
# In fixtures/categories.ymlabout: name:About# In fixtures/articles.ymlfirst: title:WelcometoRails! body:Helloworld! category:about
Fixtures - modo de uso
# this will return the User object for the fixture named david
users(:david)
# this will return the property for david called id
users(:david).id
# Returns a User instance that's not saved
user = build(:user)
# Returns a saved User instance
user = create(:user)
# Returns a hash of attributes that can be used to build a User instance
attrs = attributes_for(:user)
# Returns an object with all defined attributes stubbed out
stub = build_stubbed(:user)
# Passing a block to any of the methods above will yield the return object
create(:user) do|user|
user.posts.create(attributes_for(:post))
end