elixir-maru / maru

Elixir RESTful Framework
https://maru.readme.io
BSD 3-Clause "New" or "Revised" License
1.32k stars 85 forks source link

Testing not with ExUnit and Maru.Test is sending unhandled data #47

Closed combsco closed 7 years ago

combsco commented 7 years ago

Anyway I can suppress Maru.Test from doing that or would it be better to test with straight Plug?

defmodule CustomersSpec do
  use ESpec
  use Maru.Test, for: Auberge.API.V1.Customers
  alias Auberge.Customer

  describe "Customers API" do
    it "can create a new customer" do
      customer = %Customer{first_name: "John",
                           last_name: "Doe",
                           email: "john.doe@gmail.com",
                           phone_num: "5555555555"}

      response = build_conn()
                  |> Plug.Conn.put_req_header("content-type", "application/json")
                  |> put_body_or_params(customer |> Poison.encode!)
                  |> post("/customers")

      expected_customer = customer |> Map.put(:id, 2) |> Poison.encode!

      expect response.status |> to(eq 201)
      expect response.resp_body |> to(eq expected_customer)
    end
  end
end

Output Log

00:09:50.226 [warn]  ESpec.Runner ESpec.Runner received unexpected message in handle_info/2: {#Reference<0.0.2.559>,
 {201,
  [{"cache-control", "max-age=0, private, must-revalidate"},
   {"content-type", "application/json; charset=utf-8"}],
  "{\"phone_num\":\"5555555555\",\"last_name\":\"Doe\",\"id\":2,\"first_name\":\"John\",\"email\":\"john.doe@gmail.com\"}"}}
falood commented 7 years ago

I'm sorry, I never used ESpec before. I'll try it this weekend and respond you soon.

falood commented 7 years ago

Hi @combsco I guess you were talking about https://github.com/combsco/auberge so I test it directly.

For the Test Error

Your API returned {"updated_at":"2017-01-23T06:05:44.000000Z","phone_num":"5555555555","last_name":"Doe","inserted_at":"2017-01-23T06:05:44.000000Z","id":"823fe148-f1d1-4e08-93f5-260a819eaf50","first_name":"John","email":"john.doe@gmail.com"}

Your expected data is {"updated_at":null,"phone_num":"5555555555","last_name":"Doe","inserted_at":null,"id":2,"first_name":"John","email":"john.doe@gmail.com"}

the inserted_at and updated_at are not the same, so test failed.

For Unhandled Message

Maru depends on Plug and Plug send it. I'll look into it to find a solution, just ignore it for now.

Testing with Straight Plug

Testing with straight plug is in the plan, I'll make sure you won't change too much code to use it after it released.

Thank you for using Maru ❤️

combsco commented 7 years ago

Hey @falood

Thanks for looking into it. Yeah I haven't updated my only test in Auberge as I've changed some stuff since so that test was bound to fail.

But yeah I can just ignore the unhandled message I just wondered if it was supposed to go somewhere and how ExUnit handles it.

Thanks for developing Maru!

falood commented 7 years ago

Maybe that's what I should check. from here, I find plug process {:plug_conn, :sent} message itself, so I guess I should do the same thing for maru. I'll post it here after I find the correct way to do this 🙂

falood commented 7 years ago

Just like what plug do, receive these kind of message myself. Leave me a line if there's any other question 🙂