Updating our tests

This all looks good, but if we go back and write our tests, we're going to see a ton of failures! The reason for this is that when we were running around creating our fixtures for various tests, we never specified any values for password and password_confirmation, which is now a required value for us to be able to save any changeset. We'll start off by modifying test/vocial_web/controllers/user_controller_test.exs. The easiest thing to do to start with is setting a module attribute in VocialWeb.UserControllerTest that sets all of the valid creation parameters for a user now:

  @create_params %{
"username" => "test",
"email" => "[email protected]",
"password" => "test",
"password_confirmation" => "test"
}

Notice the addition here of password and password_confirmation (and that they have the same value). Remember that this is the set of valid creation attributes, so we want to have them in that map and matching to avoid any errors on changeset creation. Next we'll modify the GET /users/:id test and the POST /users test to use the create_params map:

  test "GET /users/:id", %{conn: conn} do
with {:ok, user} <- Vocial.Accounts.create_user(@create_params) do
conn = get conn, "/users/#{user.id}"
assert html_response(conn, 200) =~ user.username
else
_ -> assert false
end
end

test "POST /users", %{conn: conn} do
conn = post conn, "/users", %{"user" => @create_params}
assert redirected_to(conn) =~ "/users/"
end

Notice the use of @create_params as the argument to create_user/1! Rerun your tests now and you should have a few more successful test passing! We now have a few failing tests, in test/vocial/accounts/accounts_test.exs, so we'll fix those by doing essentially the same thing:

    @valid_attrs %{
username: "test",
email: "[email protected]",
active: true,
password: "test",
password_confirmation: "test"
}

def user_fixture(attrs \ %{}) do
with create_attrs <- Map.merge(@valid_attrs, attrs),
{:ok, user} <- Accounts.create_user(create_attrs)
do
user |> Map.merge(%{password: nil, password_confirmation: nil})
else
error -> error
end
end

There's a little bit of extra logic in this code: in the user_fixture function, we added a Map.merge call that sets password and password_confirmation to nil! We need to do this, because remember that those two fields are virtual fields; this means they will not receive a value when you select them out of the database! Without this code change, we'll get failures when comparing the users created to users pulled out of the database because they'll have mismatched fields. This should fix the few remaining failing tests for us, which is great! We'll also want to buff up our tests a little bit to include scenarios where we don't pass in passwords or where the password and password confirmation fields are mismatched:

    test "create_user/1 fails to create the user without a password and password_confirmation" do
{:error, changeset} = user_fixture(%{password: nil, password_confirmation: nil})
assert !changeset.valid?
end

test "create_user/1 fails to create the user when the password and
the password_confirmation don't match" do
{:error, changeset} = user_fixture(%{password: "test",
    password_confirmation: "fail"})
assert !changeset.valid?
end

Rerunning our tests now, we should see 20 tests with 0 failures! Now we can be confident about the behavior and functionality of our tests, so we should update the UI to include the password and password confirmation fields as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset