Test is using name of test like a function inside of the test

Hello I’m having troubles with a test, I’m trying mock the call to s3 to write the test, so when I run the tests, the test fail and show this error.

test upload file to s3 the file was upload correctly (DirectHomeApi.Aws.S3Test)
     test/direct_home_api/aws/s3_test.exs:11
     ** (FunctionClauseError) no function clause matching in anonymous fn/1 in DirectHomeApi.Aws.S3Test."test upload file to s3 the file was upload correctly"/1

     The following arguments were given to anonymous fn/1 in DirectHomeApi.Aws.S3Test."test upload file to s3 the file was upload correctly"/1:
     
         # 1
         "image"
     
     code: assert DirectHomeApi.Aws.MockS3.upload_files("image") ==
     stacktrace:
       test/direct_home_api/aws/s3_test.exs:14: anonymous fn/1 in DirectHomeApi.Aws.S3Test."test upload file to s3 the file was upload correctly"/1
       test/direct_home_api/aws/s3_test.exs:24: (test)

This is my module S3

defmodule DirectHomeApi.Aws.S3 do
  @callback upload_files(arg :: any) :: {:ok, map()} | {:ok, map()}

  def upload_files(image) do
    content_type = image.content_type
    file_name = image.filename
    file_path = image.path
    bucket = System.get_env("BUCKET_NAME")

    ExAws.S3.put_object(bucket, file_name, File.read!(file_path), [
      {:content_type, content_type}
    ])
    |> ExAws.request()
  end
end

test_helper.ex

ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(DirectHomeApi.Repo, :manual)
Mox.defmock(DirectHomeApi.Aws.MockS3, for: DirectHomeApi.Aws.S3)

and this is my test

defmodule DirectHomeApi.Aws.S3Test do

import Mox
use ExUnit.Case, async: true
doctest DirectHomeApi.Aws.S3

 setup :verify_on_exit!

  describe "upload file to s3" do
    test "the file was upload correctly" do
      
      DirectHomeApi.Aws.MockS3
      |> expect(:upload_files, fn {_image} -> {:ok, "response"} end)

      File.open("some_path", [:write])

      image = %Plug.Upload{
        content_type: "image/jpeg",
        filename: "test.jpeg",
        path: "some_path"
      }
      
      assert DirectHomeApi.Aws.MockS3.upload_files("image") ==
              {:error, "response"}
    end
  end
end

Can someone help me with this?

The clue is The following arguments were given to anonymous fn/1

You have |> expect(:upload_files, fn {_image} -> {:ok, "response"} end) but it seems like the argument is "image" not {"image"}.

2 Likes

Ohhh this is right, thanks Ben.