Testing: How to approach testing

Im trying to write some tests for my ash resources, and want to test errors in particular, is there a recommended way of going about this? The testing docs are a bit anemic, wonder if folks have some strategies here. In particular when matching on ash errors, I have to match on large error structs with the stacktrace. Is there a simplier way of going about this?
5 Replies
gordoneliel
gordonelielOP2y ago
Easy hack to get it to work,
{:error, %Ash.Error.Invalid{errors: errors}}

clean_errors = Enum.map(errors, &Ash.Error.clear_stacktraces/1)

assert clean_errors == [
%Ash.Error.Changes.Required{...match fields except stacktrace},
...other errors
]
{:error, %Ash.Error.Invalid{errors: errors}}

clean_errors = Enum.map(errors, &Ash.Error.clear_stacktraces/1)

assert clean_errors == [
%Ash.Error.Changes.Required{...match fields except stacktrace},
...other errors
]
Robert Graff
Robert Graff2y ago
This interests me. Right now we're doing:
assert {:error, %Ash.Error.Invalid{}} =
User.update_password(user, %{
password: new_password,
password_confirmation: new_password <> "oops",
current_password: wrong_password
})
assert {:error, %Ash.Error.Invalid{}} =
User.update_password(user, %{
password: new_password,
password_confirmation: new_password <> "oops",
current_password: wrong_password
})
and
assert {:error, %Ash.Error.Forbidden{}} =
User.update_password(
user,
%{
password: new_password,
password_confirmation: new_password,
current_password: current_password
},
actor: unauthorized_user
)
assert {:error, %Ash.Error.Forbidden{}} =
User.update_password(
user,
%{
password: new_password,
password_confirmation: new_password,
current_password: current_password
},
actor: unauthorized_user
)
Seeing better patterns would be useful
ZachDaniel
ZachDaniel2y ago
Personally, I just do most of the time
assert_raise Ash.Error.Forbidden, ~r/some message/, fn ->
the_thing
end
assert_raise Ash.Error.Forbidden, ~r/some message/, fn ->
the_thing
end
but I think we could definitely add some testing helpers, like assert_has_error or something like that
gordoneliel
gordonelielOP2y ago
How do you validate on a collection of errors? This is how I'm currently asserting a collection of errors
assert {:error, %Ash.Error.Invalid{errors: errors}} = MyResource.action(resource)

...assert errors == [
%Ash.Error.Changes.InvalidChanges{..assert all fields except stack},
... etc
]
assert {:error, %Ash.Error.Invalid{errors: errors}} = MyResource.action(resource)

...assert errors == [
%Ash.Error.Changes.InvalidChanges{..assert all fields except stack},
... etc
]
ZachDaniel
ZachDaniel2y ago
Yeah, I think if I was validating on a collection of errors I'd do something similar but from a testing perspective, I'd probably just do the same thing 3 times and assert that the error message contains what I want each time instead of one assertion that all 3 things are in the list

Did you find this page helpful?