peburrows / goth

Elixir package for Oauth authentication via Google Cloud APIs
http://hexdocs.pm/goth
MIT License
289 stars 111 forks source link

Setup test environment with Goth #174

Open RodolfoSilva opened 3 months ago

RodolfoSilva commented 3 months ago

Is there an easy way to mock Goth.fetch!(MyApp.Goth) when running tests? For instance, I want to use the same dummy token when this is used in test mode.

petermueller commented 2 months ago

@RodolfoSilva, or maintainers, I can make a PR. I've had to solve this a few times and there's a decent pattern that could live as a test implementation or as documentation, or as an igniter module, depending on people's preference

RodolfoSilva commented 2 months ago

@petermueller could you share this implementation? I think this PR will improve the library DX.

Cyytrus commented 1 month ago

Is there an easy way to mock Goth.fetch!(MyApp.Goth) when running tests? For instance, I want to use the same dummy token when this is used in test mode.

You can use Mock to custom your token response, like:

test "fetch token" do
    with_mock Goth, [fetch!: fn _ -> %Goth.Token{token: "XXXXX", type: "X", ...} end] do
      assert %Goth.Token{token: "XXXXX", type: "X", ...} == Goth.fetch!(MyApp.Goth)
    end
end