Skip to content

Instantly share code, notes, and snippets.

@gonzalo-bulnes
Created April 10, 2013 23:28
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 gonzalo-bulnes/5359341 to your computer and use it in GitHub Desktop.
Save gonzalo-bulnes/5359341 to your computer and use it in GitHub Desktop.
Using lambda functions to test exception raising with RSpec.
require 'spec_helper'
describe WorkPlace do
# This is the simplest (best) way to test a dependent: :destroy option
it "has one address and ensures it's destroyed when destroyed itself" do
should have_one(:address).dependent(:destroy)
end
# ... however the use this example does of lambda {} to encapsulate the code
# which is expected to raise an exception (and prevent the exception to make the example fail)
# is, IMHO, quite interesting.
it "ensures it's address is destroyed when destroyed" do
address = FactoryGirl.create(:address)
work_place = FactoryGirl.create(:work_place, address: address)
work_place.destroy
lambda { Address.find(address.id) }.should raise_error ActiveRecord::RecordNotFound
end
end
# Credit: http://stackoverflow.com/questions/4946635/rspec-not-working-or-raise-not-raising
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment