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 POST #46

Closed MainShayne233 closed 7 years ago

MainShayne233 commented 7 years ago

First off I just want to say I absolutely love this framework with all my heart.

Currently I am using this helper to test my POST requests

  use ExUnit.Case
  use Maru.Test, for: DataApi

  def post_and_respond(body, url) do
    build_conn
    |> Plug.Conn.put_req_header("content-type", "application/json")
    |> put_body_or_params(Poison.encode!(body))
    |> post(url)
    |> Map.get(:resp_body)
    |> Poison.decode
  end

This is obviously fine, but I imagine there is a better way.

falood commented 7 years ago

Thank you for loving maru ❤️ You can replace the last two lines with json_response which defined at https://github.com/elixir-maru/maru/blob/master/lib/maru/test.ex#L152 And if you test all your endpoint with the same way, just move your helper method as YouHelperModule.post_and_respond/2 within test_helper.exs, and then import the module in all your test files. I haven't make one by default because I think different people test different things, and a test module could be made very easy.

MainShayne233 commented 7 years ago

Perfect! Exactly what I was going for :).