Skip to content

Instantly share code, notes, and snippets.

@rocLv
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rocLv/50b83fa9024e70db88cd to your computer and use it in GitHub Desktop.
Save rocLv/50b83fa9024e70db88cd to your computer and use it in GitHub Desktop.
```ruby
assert_no_difference 'User.count' do
post users_path, user: { name: "",
email: "user@invalid",
password: "foo",
password_confirmation: "bar" }
end
```
Here we’ve included the params[:user] hash expected by User.new in the create action (Listing 7.24). By wrapping the post in the assert_no_difference method with the string argument ’User.count’, we arrange for a comparison between User.count before and after the contents of the assert_no_difference block. This is equivalent to recording the user count, posting the data, and verifying that the count is the same:
```ruby
before_count = User.count
post users_path, ...
after_count = User.count
assert_equal before_count, after_count
```
Although the two are equivalent, using assert_no_difference is cleaner and is more idiomatically correct Ruby.
@rocLv
Copy link
Author

rocLv commented Dec 30, 2014

test "valid signup information" do
    get signup_path
    assert_difference 'User.count', 1 do
      post_via_redirect users_path, user: { name:  "Example User",
                                            email: "user@example.com",
                                            password:              "password",
                                            password_confirmation: "password" }
    end
    assert_template 'users/show'
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment