elixir - 在 Phoenix 中创建和使用 Elixir 助手模块

标签 elixir phoenix-framework

我在我的人造博客 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/

相关文章:

elixir - 为什么我会收到此错误 `(Mix) Could not find an SCM for dependency :cowboy `

elixir - 你在 Phoenix/Elixir 中存储 secret 的位置以及推荐的位置?

elixir - Phoenix 框架中的动态模型

websocket - 测试客户端 channel 是否收到消息

elixir - “handle_call”超时

conditional-statements - Elixir 中 cond block 中条件内的绑定(bind)表达式?

elixir - 该子句无法匹配,因为前一个子句始终匹配

if-statement - 为什么 if(not nil) 给我一个 ArgumentError?

elixir - Phoenix/Elixir - validate_format() 整数失败

phoenix-framework - Elixir/Phoenix - 连接到外部数据库