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
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.
Hello! Python by Anthony Briggs(9916)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9779)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8298)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6847)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6580)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6449)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6414)
Kotlin in Action by Dmitry Jemerov(5064)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4317)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(4008)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3800)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3743)
The Ultimate iOS Interview Playbook by Avi Tsadok(3716)
