我在我的人造博客 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: module Blog.Acceptance.Post.Helpers is not loaded and could not be found
如何访问或加载测试文件中的帮助程序模块?
最佳答案
制作 test_helpers.exs
辅助模块可用,您需要使用 Code.require_file
加载它;但是,在这种情况下,phoenix 将您的项目配置为编译 .ex
test/support
中的文件正是针对这样的情况进入项目。所以如果你把你的模块放在 test/support/test_helpers.ex
,它将与您的项目一起编译并可供所有测试文件使用,而无需 Code.require_file
它。
关于elixir - 在 Phoenix 中创建和使用 Elixir 助手模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35329166/