我想创建自己的AccountCase,在其中创建一个用户和会话,并在我的测试中使用它。 我看着ConnCase,并尝试这样做:
defmodule MyAppWeb.AccountHelpers do using(opts) do user_attrs = Keyword.get(opts, :attrs, %{login: "test@gmail.com", password: "123456", locale: "en"}) quote do def fixture(:user) do {:ok, %{session: session, user: user}} = MyApp.Sessions.register_user(user_attrs) .... {:ok, %{user: user, session: session}} end end end end在测试文件中,我这样做:
defmodule MyAppWeb.V1.CategoryControllerTest do use MyAppWeb.ConnCase use MyAppWeb.AccountHelpers ... end但是当我运行测试时,出现错误
** (CompileError) test/myapp_web/controllers/v1/category_controller_test.exs:3: module MyAppWeb.AccountHelpers is not loaded and could not be found (elixir) expanding macro: Kernel.use/1如何正确创建案例?
I want to create my own AccountCase, in which I would create a user and session, and use it in my tests. I looked at ConnCase, and try to do so:
defmodule MyAppWeb.AccountHelpers do using(opts) do user_attrs = Keyword.get(opts, :attrs, %{login: "test@gmail.com", password: "123456", locale: "en"}) quote do def fixture(:user) do {:ok, %{session: session, user: user}} = MyApp.Sessions.register_user(user_attrs) .... {:ok, %{user: user, session: session}} end end end endIn the test file, I do so:
defmodule MyAppWeb.V1.CategoryControllerTest do use MyAppWeb.ConnCase use MyAppWeb.AccountHelpers ... endbut when I run the test, I get an error
** (CompileError) test/myapp_web/controllers/v1/category_controller_test.exs:3: module MyAppWeb.AccountHelpers is not loaded and could not be found (elixir) expanding macro: Kernel.use/1How correctly to create the case?
最满意答案
在Phoenix的test环境中,编译和测试.exs文件中的唯一.ex文件是lib/和test/support/ (请参阅elixirc_paths中mix.exs的定义)。 你的.ex文件在test/不会被编译,这意味着在那里定义的模块不能用于你的测试,你会得到“无法找到”的错误。 解决方案是将.ex文件移动到test/support/ 。
In test environment in Phoenix, the only .ex files compiled and therefore available in your test .exs files are the ones in lib/ and test/support/ (see the definition of elixirc_paths in your mix.exs). Your .ex file is in test/ which won't be compiled, which means the modules defined there are not available to your tests and you get the "could not be found" error. The solution is to move the .ex file to test/support/.
更多推荐
发布评论