Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating and using an Elixir helpers module in Phoenix

I have a group of acceptance tests I've created in my faux blog phoenix app. There's some duplicated logic between them I'd like to move to a helpers module to keep things DRY.

Here is the directory structure:

test/acceptance/post
├── create_test.exs
├── delete_test.exs
├── helpers.exs
├── index_test.exs
└── update_test.exs

The helpers.exs file is where I'd like to stick the duplicated acceptance test logic. It looks something like:

defmodule Blog.Acceptance.Post.Helpers do
  def navigate_to_posts_index_page do
    # some code
  end
end

Then in one of my test files, say index_test.exs, I'd like to import the helpers module to use it's methods:

defmodule Blog.Acceptance.Post.IndexTest do 
  import Blog.Acceptance.Post.Helpers
end

However, I'm getting this error:

** (CompileError) test/acceptance/post/index_test.exs:7: module Blog.Acceptance.Post.Helpers is not loaded and could not be found

How do I get access to or load the helper module in my test files?

like image 671
Elliot Larson Avatar asked Feb 11 '16 01:02

Elliot Larson


1 Answers

To make the test_helpers.exs Helpers module available, you'd need to use Code.require_file to load it; however, in this case phoenix configured your project to compile .ex files in test/support into the project exactly for cases like this. So if you place your module in test/support/test_helpers.ex, it will be compiled with your project and available to all test files without having to Code.require_file it.

like image 53
Chris McCord Avatar answered Nov 10 '22 02:11

Chris McCord