michaelfich

michaelfich

Testing ExAws.stream! with Mox

I’ve been working with a function that looks similar to the one that I’ve described below. It works adequately in practice but trying to unit test it has been a challenge to this point.

lib/my_module.ex

defmodule MyApp.MyModule do
  def my_func do
    "my-s3-bucket"
    |> ExAws.S3.list_objects_v2(prefix: "path/to/files")
    |> ExAws.stream!()
    |> Enum.map(&do_work/1)
  end
end

I’ve currently set up ExAws like the code samples below for mocking out ExAws.request! calls.

config/test.exs

config :my_app, :ex_aws,
  http_client: ExAws.Request.HttpMock

test/support/mocks.ex

Mox.defmock(ExAws.Request.HttpMock, for: ExAws.Request.HttpClient)

If anyone has suggestions for how to make the Mox.expect/4 call here properly set up the ExAws.stream! call, that would be greatly appreciated.

test/my_module_test.exs

defmodule MyApp.MyModuleTest do
  use ExUnit.Case, async: true

  import Mox

  setup :verify_on_exit!

  describe "my_func/0" do
    test "mocks api call(s)" do
      # how do we mock the stream?
      expect(ExAws.Request.HttpMock, ..., fn ..., ... ->
        # unsure
      end)

      assert [_ | _] = my_func()
    end
  end
end

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

To mock at the HTTP layer you need to return a response shape that is the same as what AWS itself returns. the stream function is just making repeated object list calls with a cursor, so you’ll need to return that shape.

I don’t however recommend mocking at this layer. You’ll spend more time trying to make ExAws internals work. Rather I would make an internal FileStore behavior in your app and then have a mock of that which can return a stream directly.

Also Liked

LostKobrakai

LostKobrakai

That – but also if you eventually want to run integration tests you can consider pointing it against minio or localstack instances.

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement