redding / assert

Assertion style testing framework.
https://github.com/redding/assert
MIT License
10 stars 1 forks source link

assert_difference??? #283

Closed kellyredding closed 3 years ago

kellyredding commented 7 years ago

(from http://www.justinweiss.com/articles/the-lesser-known-features-in-rails-5-dot-1/)

Assert more than just differences

I love assert_difference. Before assert_difference, I spent way too much time juggling local variables in tests:

old_score = @user.score
@user.answer_question!(...)
assert_equal old_score + 10, @user.score

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??).

kellyredding commented 3 years ago

Closing in favor of doing #301 .