开发者

What is the difference between these two tests causing one to fail and one to pass?

开发者 https://www.devze.com 2023-04-05 14:21 出处:网络
I have the following two tests for a Rails app I\'m working on. This one fails: test \"should save comment without parent comment\" do

I have the following two tests for a Rails app I'm working on.

This one fails:

test "should save comment without parent comment" do
  comment = Comment.new(:text => "hello world")
  comment.user = users(:charlie)
  comment.story = stories(:one)
  assert comment.save
end

And this one passes:

test "should save comment without parent comment" do
  comment = Comment.new(:text => "hello world")
  comment.user = users(:charlie)
  comment.story = stories(:one)

  comment.save

  assert comment.save
end

When I change the assert line in the first (fai开发者_高级运维ling) test to this:

assert comment.save, Proc.new { "#{comment.errors.messages.inspect} -- valid? #{comment.valid?} -- save? #{comment.save}" }

It prints out this as the error message:

{} -- valid? true -- save? true

I'm completely at a loss as to why my Comment model requires two calls to save. It works fine in my controller with just one call to save.

I have an after_create callback, but I don't think it should be affecting anything (as I said, this all works fine from my controller):

after_create :create_upvote_from_user
def create_upvote_from_user
  Vote.create! :user => user, :vote => 1, "#{self.class.name.underscore}_id" => id
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号