-
Notifications
You must be signed in to change notification settings - Fork 86
Refactoring Project Category Controller Tests #420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a couple of questions, but this looks great! 👍
|
||
first_result | ||
|> assert_result_id(project_category_1.id) | ||
|> assert_jsonapi_relationship("project", project.id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we no longer want to test the relationships are valid?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think since they are already tested in the model test, we decided to take them out of the controller tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you agree with that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense to me. Thanks for clarifying!
data = conn |> post(path, payload) |> json_response(422) | ||
assert data["errors"] != %{} | ||
test "renders 422 when data is invalid", %{conn: conn} do | ||
invalid_attrs = %{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to assign invalid_attrs
? On the two tests below we simply call request_create(%{})
. Thoughts on removing this or possibly declaring @invalid_attrs
up top and reusing it in the tests below as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the other controller tests we were declaring @invalid_attrs at the top and using it in the 422 test, but since it's the only place we end up using it @joshsmith said this might be a little cleaner.
I think this difference with the request_create
for the 401 and 403 tests we don't even hit the attributes before it halts with an error, so an empty attribute map is kind of just placeholding instead of being invalid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha. Do we need to even pass anything in the tests below, then? Can we just do conn |> request_create |> json_response(401)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mackenziehicks and I were asking all the same questions yesterday! In the api_case helper for request_create
you have to pass in attributes, unlike for request_delete where you have an option to just use the conn.
def request_create(conn, attrs) do
path = conn |> path_for(:create)
payload = json_payload(factory_name, attrs)
conn |> post(path, payload)
end
def request_delete(conn), do: conn |> request_delete(default_record)
def request_delete(conn, :not_found), do: conn |> request_delete(-1)
def request_delete(conn, resource_or_id) do
path = conn |> path_for(:delete, resource_or_id)
conn |> delete(path)
end
end
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, thanks for explaining!
What are your thoughts on adding a default to request_create
?
def request_create(conn, attrs \\ %{})
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I'm dumb and hadn't considered this. Yes, this makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joshsmith I think we should add it for update too.
Right now the test looks like this:
assert conn |> request_update({@valid_attrs}) |> json_response(401)
but we haven't actually added anything to update, so do we need to pass in @valid_attrs
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'd add it for update
, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor change and then good for me.
project = insert(:project) | ||
project_category_1 = insert(:project_category, project: project, category: arts) | ||
project_category_2 = insert(:project_category, project: project, category: society) | ||
[project_category_1, project_category_2] = insert_pair(:project_category) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as comment, while this is good, it could be rewritten in one line as
[project_category_1, project_category_2 | _] = insert_list(3, : project_category_1)
We don't really need to pre-insert the category, or the project. The factory should do that part for us.
a9592cb
to
b3a3f2e
Compare
b3a3f2e
to
3358049
Compare
What's in this PR?
Adding api_case.ex helpers.
References
Closes #418
Progress on: #413