我在我的phoenix项目中使用mock来测试控制器和存储库之间的交互。
我在我的控制器中写了这个测试:
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这是实际的代码:
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输出如下:
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你能帮我弄清楚问题出在哪里吗?如何测试这种交互?
发布于 2015-07-28 23:04:14
传递给操作的参数是从字符串到字符串的映射,因为框架无法知道哪些参数是字符串,哪些是数字。因此,您需要显式地将参数转换为所需的类型。尝试:
board_id = String.to_integer(board_id)
columns = Repo.all from(c in Column, where: c.board_id == ^board_id)https://stackoverflow.com/questions/31679443
复制相似问题