Noah Clark
Noah Clark

Reputation: 8131

What's Different Between These Two Tests (one fails, one passes)

Here is the passing test:

test "should create user" do
        assert_difference('User.count') do
        post :create, user: { name: "John Doe", 
                              email: "[email protected]",
                              password: "foobar3",
                              password_confirmation: "foobar3" }
end

And here is the failing test:

def setup
  @user = User.new(name: "John Doe", 
               email: "[email protected]",
               password: "foobar3",
               password_confirmation: "foobar3")
end


test "should create user" do
        assert_difference('User.count') do
        post :create, user: @user 
end

Why does the second test fail? and how can I make it use the @user variable?

Upvotes: 0

Views: 35

Answers (1)

MrDanA
MrDanA

Reputation: 11647

Use the .attributes method to get a hash of just the user's attributes, so you send those along instead of the actual user object.

test "should create user" do
    assert_difference('User.count') do
    post :create, user: @user.attributes
end

Upvotes: 1

Related Questions