Skip to content

Instantly share code, notes, and snippets.

@dasibre
Created June 11, 2016 15:01
Show Gist options
  • Save dasibre/4553ff15d1fd14ad1fab3d07b72f6e9c to your computer and use it in GitHub Desktop.
Save dasibre/4553ff15d1fd14ad1fab3d07b72f6e9c to your computer and use it in GitHub Desktop.
rspec idioms for cleaner tests
describe Thing do
def given_thing_with(options)
yield Thing.new do |thing|
thing.set_status(options[:status])
end
end
it "should do something when ok" do
given_thing_with(:status => 'ok') do |thing|
thing.do_fancy_stuff(1, true, :move => 'left', :obstacles => nil)
...
end
end
it "should do something else when not so good" do
given_thing_with(:status => 'not so good') do |thing|
thing.do_fancy_stuff(1, true, :move => 'left', :obstacles => nil)
...
end
end
end
@dasibre
Copy link
Author

dasibre commented Jun 11, 2016

This sort of indirection can make under- standing failures quite painful when overused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment