Skip to content

Instantly share code, notes, and snippets.

@developish
Created October 14, 2011 07:25
Show Gist options
  • Save developish/1286469 to your computer and use it in GitHub Desktop.
Save developish/1286469 to your computer and use it in GitHub Desktop.
Test ActiveRecord #delete vs #destroy
class Post < ActiveRecord::Base
before_destroy :protect_from_destruction
def protect_from_destruction
if protected?
errors.add(:base, "Whoa this one is important")
false
end
end
def protected?
true
end
end
require 'test_helper'
class PostTest < ActiveSupport::TestCase
test "using delete deletes objects despite callbacks" do
post = Post.create
post.delete
assert_equal Post.first, nil
end
test "using destroy respects callback" do
post = Post.create
post.destroy
assert_equal post.errors[:base][0], "Whoa this one is important"
assert_equal post, Post.first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment