user601836
user601836

Reputation: 3235

mocking in phoenix framework

I am using mock in my phoenix project to test the interaction between a controller and the repo.

I wrote this test in my controller:

test_with_mock "list all entries on index", %{conn: conn}, Repo, [:passthrough], [] do
  conn = get conn, board_column_path(conn, :index, 1)

  assert called Repo.all from(c in Column, where: c.board_id == 1)
  assert html_response(conn, 200) =~ "Listing columns"
end

And this is the actual code:

def index(conn, %{"board_id" => board_id}) do
    columns = Repo.all from(c in Column, where: c.board_id == ^board_id)
    render(conn, "index.html", columns: columns)
end

The output is the following:

1) test list all entries on index (SimpleBoard.ColumnControllerTest)
test/controllers/column_controller_test.exs:17
Expected truthy, got false
code: called(Repo.all(from(c in Column, where: c.board_id() == 1)))
stacktrace:
  test/controllers/column_controller_test.exs:20

Can you help me to understand where the problem is? How do you test this kind of interaction?

Upvotes: 1

Views: 363

Answers (1)

Paweł Obrok
Paweł Obrok

Reputation: 23164

The params passed to an action are a map from strings to strings as there's no way for the framework to know which params were intended to be strings and which numbers. Because of that you need to explicitly convert your params to the type you require. Try:

board_id = String.to_integer(board_id)
columns = Repo.all from(c in Column, where: c.board_id == ^board_id)

Upvotes: 1

Related Questions