Created
October 14, 2011 07:25
-
-
Save developish/1286469 to your computer and use it in GitHub Desktop.
Test ActiveRecord #delete vs #destroy
This file contains hidden or 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
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 |
This file contains hidden or 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 '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