Hello everyone, I’m a beginner programmer, I can’t write a Test, can you please tell me!?
given a function, you need to write a test for it:
def add_driver_incident_message(%{driver_incident_id: driver_incident_id, body: body}) do
%{driver_incident_id: driver_incident_id, body: body}
|> DriverIncidentMessage.changeset()
|> Repo.insert()
end
Thanks!
NobbZ
March 1, 2023, 10:52am
2
Hi!
Would you mind telling us, what you have tried so far?
And what you actually want to test?
3 Likes
check whether the driver incident message adds
test "add_driver_incident_message/2 returns a driver_incident changeset
driver_incident = Factory.insert!(:driver_incident)
assert {:ok, %DriverIncidentMessage{}} =
Incidents.add_driver_incident_message(%{driver_incident_id: driver_incident_id, body: body})
returned_driver_incident =
Incidents.add_driver_incident_message(%{driver_incident_id: driver_incident_id, body: body})
end
def make(:driver_incident, fields) do
fields
|> put_new_field(:id, lazy(make_id()))
|> put_new_field(:ride, lazy(make(:ride)))
|> put_new_field(:ride_id, & &1.ride.id)
|> Map.put_new(:messages, [])
|> create_struct(DriverIncident)
end
NobbZ
March 1, 2023, 11:16am
4
That test is missing:
a closing double quote for the label
an opening do
after the label
a closing end
after the test.
Also the final assignment in the test has no real use, as the value is forgotten right at the end of the test.
If those mentioned problems are not the actual problems you have with the test, please describe your problem in more detail.
2 Likes
we have, incident id and text from passenger (body)
need to check:
with data (driver_incident_id: driver_incident_id, body: body)
uploaded to repository > Repo.insert().
with valid data creates an incident message, if the data is not correct it shows an error
I didn’t write the test correctly, maybe.
Well at the end of your test you can just get the driver incident object from the DB and check if the fields match the data in the earlier stage (that you used to insert it with).
1 Like