Created
April 10, 2013 23:28
-
-
Save gonzalo-bulnes/5359341 to your computer and use it in GitHub Desktop.
Using lambda functions to test exception raising with RSpec.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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