Testing Elixir by Andrea Leopardi & Jeffrey Matthias

Testing Elixir by Andrea Leopardi & Jeffrey Matthias

Author:Andrea Leopardi & Jeffrey Matthias [Andrea Leopardi]
Language: eng
Format: epub
Publisher: Pragmatic Bookshelf
Published: 2021-07-12T16:00:00+00:00


Creating a SchemaCase for Shared Test Code

Case templates are very useful when we identify a pattern for test files that would benefit from shared code. Our refactors on the basic schema tests created two helper functions, valid_params/1 and invalid_params/1, that would be good to move into a common place to be reused by all our schema tests. We’ll create a case template called SchemaCase and move the helper functions there. Create a new file called testing_ecto/test/schema_case.ex and add the basic structure for a case template:

testing_ecto/test/schema_case.ex

​ ​defmodule​ TestingEcto.SchemaCase ​do​

​ ​use​ ExUnit.CaseTemplate

​

​ using ​do​

​ ​quote​ ​do​

​ alias Ecto.Changeset

​ ​import​ TestingEcto.SchemaCase

​ ​end​

​ ​end​

​

​ ​end​

For now, it’s pretty simple. We can alias Ecto.Changeset into the case template because all future schema tests will need it. We have import TestingEcto.SchemaCase included; so when we add the helper functions, anything that “uses” the template will get those functions. Our last step is to copy the two functions, valid_params/1 and invalid_params/1, into the file, below the “using” block. Just be sure to make both functions public (def instead of defp), as import only works with public functions. The case template is now ready to go, but we need to make sure it’s compiled in our app when running tests. We’ll also make one last refactor pass on our test file, making sure it uses the case template and no longer contains the code locally.

To make sure the case template is available in the application when it runs tests, let’s modify our mix.exs file. Open it up and add elixirc_paths: elixirc_paths(Mix.env()), into the list in the project section:

testing_ecto/mix.exs

​ ​def​ project ​do​

​ [

​ ​app:​ ​:testing_ecto​,

​ ​version:​ ​"​​0.1.0"​,

​ ​elixir:​ ​"​​~> 1.10"​,

​ ​start_permanent:​ Mix.env() == ​:prod​,

​ ​elixirc_paths:​ elixirc_paths(Mix.env()),

​ ​deps:​ deps()

​ ]

​ ​end​

That’s calling a function that doesn’t exist yet, elixirc_paths/1, so we need to add that function as well. Above the deps section of your mix file, add the following function heads:

testing_ecto/mix.exs

​1: ​defp​ elixirc_paths(​:test​), ​do​: [​"​​lib"​, ​"​​test"​]

​2: ​defp​ elixirc_paths(_), ​do​: [​"​​lib"​]

The line that matters the most here is line 1, where we’re telling the application to include all the files in the test directory when compiling in the test environment. This will make the SchemaCase file available in the :test environment while keeping it out of the :dev and :prod environments, or any other environments added later.

Now we just need to refactor the test file. Instead of changing the code in place, we’ll make a copy of testing_ecto/test/schemas/user_basic_schema_2_test.exs, calling it testing_ecto/test/schemas/user_basic_schema_3_test.exs. Ignoring the tests, nested inside of the describe blocks, your file will look like the following code:

testing_ecto/test/schemas/user_basic_schema_3_test.exs

​1: ​defmodule​ TestingEcto.Schemas.UserBasicSchema3Test ​do​

​- ​use​ TestingEcto.SchemaCase

​- alias TestingEcto.Schemas.UserBasicSchema

​-

​5: @expected_fields_with_types [

​- {​:date_of_birth​, ​:date​},

​- {​:email​, ​:string​},

​- {​:favorite_number​, ​:float​},

​- {​:first_name​, ​:string​},

​10: {​:last_name​, ​:string​},

​- {​:phone_number​, ​:string​}

​- ]

​- @optional [​:favorite_number​]

​-

​15: describe ​"​​fields and types"​ ​do​

​- ​end​

​-

​- describe ​"​​changeset/1"​ ​do​

​- ​end​

​20: ​end​

As before, make sure to rename your test module, upping the number to 3 (line 1) to avoid naming conflicts. Replace the two lines containing use ExUnit.Case and alias Ecto.Changeset with use TestingEcto.SchemaCase, as seen on line 2. Now, since SchemaCase has the valid_params/1 and invalid_params/1 functions, you can delete them from the file.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.