Skip to content
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

Mock helpers #194

Closed
9 tasks done
teamon opened this issue Mar 22, 2018 · 3 comments
Closed
9 tasks done

Mock helpers #194

teamon opened this issue Mar 22, 2018 · 3 comments

Comments

@teamon
Copy link
Member

teamon commented Mar 22, 2018

Currently to mock a JSON response one has to write:

Tesla.Mock.mock fn env ->
  {:ok, %Tesla.Env{status: 200, body: ~s|{"ok": true}|, headers: [{"content-type", "application/json}]}
end

Since this (and some other) cases are very common it would be useful to have:

Tesla.Mock.mock fn env ->
  json(%{"ok" => true})
end

or

Tesla.Mock.mock fn env ->
  json(%{"ok" => true}, status: 201)
end

Also, returning simple {200, [], "hello"} tuple instead of full Tesla.Env might be handy.

Right now the proposal is to have:

  • Handle other return types with the following rules:
    • Tesla.Env => {:ok, Tesla.Env}
    • {status, headers, body} => {:ok, Tesla.Env{...}}
    • other => {:error, other}
  • Add utility functions:
    • json(body, opts \\ []) :: Tesla.Env (status defaults to 200, set proper content type header)
    • text(body, opts \\ []) :: Tesla.Env (status defaults to 200, set proper content type header)
  • Provide JSON engine configuration (config :tesla, Tesla.Mock, json_engine: Jason)
  • Update REAMDE (with import Tesla.Mock.Helpers line)
@teamon teamon added this to the 1.1 milestone Mar 22, 2018
teamon added a commit that referenced this issue Mar 30, 2018
@teamon teamon closed this as completed Mar 30, 2018
@florinpatrascu
Copy link

hi @teamon - can I configure a different json engine, programmatically? I am porting my projects to 1.0.0-beta.1, but some of them have "hard" dependencies on Poison, so I have to stick with the latter for time being. ty!

@teamon
Copy link
Member Author

teamon commented May 23, 2018

config :tesla, Tesla.Mock, json_engine: Poison

@florinpatrascu
Copy link

hah, I was thrown on a wrong path being mislead by the Tesla.Mock, thinking you're planning to use it for tests (internally) only :) Thank you!

@teamon teamon modified the milestones: 1.1, 1.0 Jun 7, 2018
@teamon teamon removed this from the 1.2 milestone Jul 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants