ExUnit自己的案例(ExUnit own case)

编程入门 行业动态 更新时间:2024-10-24 04:29:25
ExUnit自己的案例(ExUnit own case)

我想创建自己的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 end

In the test file, I do so:

defmodule MyAppWeb.V1.CategoryControllerTest do use MyAppWeb.ConnCase use MyAppWeb.AccountHelpers ... end

but 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/1

How 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/.

更多推荐

本文发布于:2023-08-07 11:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464261.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自己的   案例   ExUnit   case

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!