Nick
Nick

Reputation: 3090

Why does this integration test with post request fail?

I have written an integration test for a signup form that writes to two tables/models. It fails with the message "Organization.count" didn't change by 1. Is there, however, a way to see with what kind of error message the test fails (the form works in development, although there is an issue with displaying error messages, so I don't understand why the test fails). So I mean the error message you would have seen if you had done the same exact thing on the server to help me find out why it didn't save the organization/user.

  test "valid combined organization user signup" do
    get new_path
    assert_template 'organizations/new'
    assert_difference ['Organization.count', 'User.count'], 1 do
      post organizations_path, organization: { name: "Abc1",
                                               bag: "aef1",
                            users_attributes: [email: "[email protected]",
                                               username: "abc1",
                                               password: "foobar",
                                               password_confirmation: "foobar"] }
       end
  end

Perhaps the test log can help? I see here IS NULL LIMIT for user email and username. I don't understand why, given the test syntax...

Started POST "/organizations" 
Processing by OrganizationsController#create as HTML
  Parameters: {"organization"=>{"name"=>"Abc def 1", "bag"=>"adef1", "users_attributes"=>[{"email"=>"[email protected]", "username"=>"adef1", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}]}}
  [1m[36m (0.2ms)[0m  [1mSAVEPOINT active_record_1[0m
  [1m[35mOrganization Exists (0.6ms)[0m  SELECT  1 AS one FROM "organizations" WHERE LOWER("organizations"."name") = LOWER('Abc def 1') LIMIT 1
  [1m[36mOrganization Exists (0.3ms)[0m  [1mSELECT  1 AS one FROM "organizations" WHERE LOWER("organizations"."bag") = LOWER('adef1') LIMIT 1[0m
  [1m[35mOrganization Exists (0.3ms)[0m  SELECT  1 AS one FROM "organizations" WHERE LOWER("organizations"."name") = LOWER('Abc def 1') LIMIT 1
  [1m[36mOrganization Exists (0.3ms)[0m  [1mSELECT  1 AS one FROM "organizations" WHERE LOWER("organizations"."bag") = LOWER('adef1') LIMIT 1[0m
  [1m[35mSQL (0.4ms)[0m  INSERT INTO "organizations" ("name", "bag", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["name", "Abc def 1"], ["bag", "adef1"], ["created_at", "2015-06-27 16:28:56.179789"], ["updated_at", "2015-06-27 16:28:56.179789"]]
  [1m[36mUser Exists (0.6ms)[0m  [1mSELECT  1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1[0m
  [1m[35m (0.6ms)[0m  SELECT "users"."email" FROM "users"  ORDER BY "users"."username" ASC
  [1m[36mUser Exists (0.2ms)[0m  [1mSELECT  1 AS one FROM "users" WHERE "users"."username" IS NULL LIMIT 1[0m
  [1m[35m (0.2ms)[0m  ROLLBACK TO SAVEPOINT active_record_1

The controller method is posted here: stackoverflow.com/q/31072646/4499505 (organizations_path refers to the create method)

If I compare the test log with the development log (since in development the form does work), for development it makes the post below, which seem to differ from the post in the test log. How should I adjust the test syntax to create the same post as in development?

Parameters: {"utf8"=>"✓", "authenticity_token"=>"***", "organization"=>{"name"=>"test60", "bag"=>"tes60", "users_attributes"=>{"0"=>{"email"=>"[email protected]", "username"=>"test60", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}}, "commit"=>"Register"}

Update: To match the development log, I changed the post line within the test to:

post organizations_path, organization: { name: "Abc def 1",
                                         bag: "adef1",
             users_attributes: { "0" => {email: "[email protected]",
                                         username: "adef1",
                                         password: "foobar",
                                         password_confirmation: "foobar"}} }
end

Now in the test log it no longer has IS NULL LIMIT but it still rolls back and thus the test still fails with the same message:

  [1m[35mSQL (0.5ms)[0m  INSERT INTO "organizations" ("name", "bag", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["name", "Abc def 1"], ["bag", "adef1"], ["created_at", "2015-06-27 19:49:45.430750"], ["updated_at", "2015-06-27 19:49:45.430750"]]
  [1m[36mUser Exists (0.4ms)[0m  [1mSELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1[0m
  [1m[35m (0.5ms)[0m  SELECT "users"."email" FROM "users"  ORDER BY "users"."username" ASC
  [1m[36mUser Exists (0.5ms)[0m  [1mSELECT  1 AS one FROM "users" WHERE LOWER("users"."username") = LOWER('adef1') LIMIT 1[0m
  [1m[35m (0.2ms)[0m  ROLLBACK TO SAVEPOINT active_record_1

Upvotes: 2

Views: 73

Answers (1)

Rob Wise
Rob Wise

Reputation: 5120

Your test code is not the same as what your development code is doing. You need to add brackets inside of the users_attributes array:

test "valid combined organization user signup" do
  get new_path
  assert_template 'organizations/new'
  assert_difference ['Organization.count', 'User.count'], 1 do
    post organizations_path, 
         organization: { name: "Abc1",
                         bag: "aef1",
                         users_attributes: { "0" => { email: "[email protected]",
                                                      username: "abc1",
                                                      password: "foobar",
                                                      password_confirmation: "foobar" } } }
  end
end

This is why your log says it is trying to find a user whose email IS NULL, because it wasn't able to retrieve the email attribute properly, so it just defaulted to NULL instead of [email protected].


Edit: updated to match working syntax. OP was also reporting that once the syntax was fixed, there was an error with the User model not passing a minimum length validation on username which he fixed by simply using a longer username in the test data.

Upvotes: 1

Related Questions