[英]Creating and using an Elixir helpers module in Phoenix
我在我的虚拟博客凤凰应用程序中创建了一组验收测试。 他们之间有一些重复的逻辑我想转移到帮手模块以保持干燥。
这是目录结构:
test/acceptance/post
├── create_test.exs
├── delete_test.exs
├── helpers.exs
├── index_test.exs
└── update_test.exs
helpers.exs
文件是我想坚持重复验收测试逻辑的地方。 它看起来像:
defmodule Blog.Acceptance.Post.Helpers do
def navigate_to_posts_index_page do
# some code
end
end
然后在我的一个测试文件中,比如index_test.exs
,我想导入helpers模块来使用它的方法:
defmodule Blog.Acceptance.Post.IndexTest do
import Blog.Acceptance.Post.Helpers
end
但是,我收到此错误:
**(CompileError)test / acceptance / post / index_test.exs:7:模块Blog.Acceptance.Post.Helpers未加载且无法找到
如何在我的测试文件中访问或加载帮助程序模块?
要使test_helpers.exs
Helpers模块可用,您需要使用Code.require_file
来加载它; 但是,在这种情况下,phoenix将您的项目配置为将test/support
.ex
文件编译到项目中,完全适合这样的情况。 因此,如果将模块放在test/support/test_helpers.ex
,它将与您的项目一起编译,并且可用于所有测试文件,而无需Code.require_file
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.