Skip to content

Instantly share code, notes, and snippets.

@niku
Created March 6, 2011 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niku/857289 to your computer and use it in GitHub Desktop.
Save niku/857289 to your computer and use it in GitHub Desktop.
How do I use expect{}.to change().from().to() ?
require 'rspec'
class Foo
attr_reader :ary
def initialize
@ary = []
@num = 0
end
def update
@ary << @num
@num += 1
end
end
describe Foo do
describe "#update" do
context 'when first update' do
subject{ Foo.new }
it do
expect{ subject.update }.to change(subject, :ary).from([]).to([0])
end
end
end
end
# result this
# /Users/niku/junk/2011/03% rspec --version
# 2.5.1
# /Users/niku/junk/2011/03% rspec --color --format documentation foo.rb
#
# Foo
# #update
# when first update
# should change #ary (FAILED - 1)
#
# Failures:
#
# 1) Foo#update when first update
# Failure/Error: expect{ subject.update }.to change(subject, :ary).from([]).to([0])
# ary should have initially been [], but was [0]
# # ./foo.rb:20:in `block (4 levels) in <top (required)>'
#
# Finished in 0.00066 seconds
# 1 example, 1 failure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment