With assert_difference, it’s much clearer what you’re trying to do:
assert_difference "@user.score", 10 do
@user.answer_question!(...)
end
In Rails 5.1, assert_changes takes this one step further. assert_difference only checks changes in count. But assert_changes can check non-numerical changes, like changes between two strings, or between nil and something else:
assert_changes "users(:justin).name", from: "Justin", to: "Bob" do
@user.update_attributes(name: "Bob")
end
Instead of a string, you can give it a lambda:
assert_changes -> { users(:justin).name }, from: "Justin", to: "Bob" do
@user.update_attributes(name: "Bob")
end
to: can be anything that compares with ===. That’s nice when you know something about the value, but don’t know what it is, specifically:
assert_changes -> { users(:justin).updated_at }, to: ActiveSupport::TimeWithZone do
@user.update_attributes(name: "Bob")
end
Just an idea - maybe helpful, maybe not. Curious how those strings are getting eval'd (eval??).
(from http://www.justinweiss.com/articles/the-lesser-known-features-in-rails-5-dot-1/)
Just an idea - maybe helpful, maybe not. Curious how those strings are getting eval'd (eval??).