Test count of association, not class
When testing, you might do something like:
expect { @user.make_widget }.to change { Widget.count }.by(1)
Don't. There might be some reason why the new widget's user_id doesn't get set. Instead, test:
expect { @user.make_widget }.to change { @user.widgets.count }.by(1)
That way you're only looking at the widgets that actually belong to the user.
Written by Dave Aronson
Related protips
1 Response
If you've scoped your controllers to only return Widgets owned by the current user, an integration test should pick up a failure. But, then again, you should probably have authorization tests for that, and this definitely works in a unit test. Great tip!
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Related Tags
#testing
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#