{"id":5007,"date":"2016-01-21T07:00:08","date_gmt":"2016-01-21T09:00:08","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=5007"},"modified":"2016-01-28T12:34:10","modified_gmt":"2016-01-28T14:34:10","slug":"writing-acceptance-tests-in-phoenix","status":"publish","type":"post","link":"http:\/\/blog.plataformatec.com.br\/2016\/01\/writing-acceptance-tests-in-phoenix\/","title":{"rendered":"Writing Acceptance tests in Phoenix"},"content":{"rendered":"

Acceptance testing seems to be in its first steps in the Elixir ecosystem, but there are already some cool libs that can help us out to do it. I’m going to show you how we did it with Hound<\/a>.<\/p>\n

In this blog post, we’ll write a few acceptance tests for an expenses report listing page, where we’ll interact with the report and some form elements.<\/p>\n

To make possible to interact with the elements on the page, we’ll need a web browser driver. Hound accepts chrome_driver<\/code>, firefox<\/code>, phantomjs<\/code> and selenium<\/code>. My choice is phantomjs<\/code> because I want to use a headless browser (I don’t want the driver to open a browser during the execution of the test suite).<\/p>\n

Setup<\/h2>\n

First we’ll need to add Hound into our dependencies, so add the following line in your mix.exs<\/code>:<\/p>\n

{:hound, \"~> 0.8\"}\n<\/code><\/pre>\n

Make sure it’ll start during the test suite runtime. To do this we’ll need to add Application.ensure_all_started(:hound)<\/code> before ExUnit.start<\/code> in our test helper:<\/p>\n

Application.ensure_all_started(:hound)\nExUnit.start\n<\/code><\/pre>\n

We’ll be using phantomjs<\/code> as our web driver. Make sure it’s properly installed and that you can start it<\/a> with phantomjs --wd<\/code>. To configure it, add this to the config.exs<\/code> file:<\/p>\n

config :hound, driver: \"phantomjs\"\n<\/code><\/pre>\n

Take a look at this doc from Hound resources<\/a> to check if you’d like different configs.<\/p>\n

We’ll also need to set the server config in our config\/test.exs<\/code>to true<\/code>.<\/p>\n

config :my_app, MyApp.Endpoint,\n  http: [port: 4001]\n  server: true\n<\/code><\/pre>\n

That should do it! Before writing our first test, let’s define an IntegrationCase<\/code> module, similar to the ModelCase<\/code> and ConnCase<\/code> provided by Phoenix, which will include all functionality we need to write our integration tests. Create the test\/support\/integration_case.ex<\/code> file and add the following content:<\/p>\n

defmodule MyApp.IntegrationCase do\n  use ExUnit.CaseTemplate\n\n  using do\n    quote do\n      use Hound.Helpers\n\n      import Ecto.Model\n      import Ecto.Query, only: [from: 2]\n      import MyApp.Router.Helpers\n\n      alias MyApp.Repo\n\n      # The default endpoint for testing\n      @endpoint MyApp.Endpoint\n\n      hound_session\n    end\n  end\n\n  setup tags do\n    unless tags[:async] do\n      Ecto.Adapters.SQL.restart_test_transaction(MyApp.Repo, [])\n    end\n\n    :ok\n  end\nend\n<\/code><\/pre>\n

There are a few lines that are worth commenting:<\/p>\n