View Source Beacon.Test (Beacon v0.2.1)

Testing utilities to create and assert Beacon resources.

Usage

First you need to activate the :testing mode in your site configuration:

# test.exs
# active testing mode for all sites under test
config :my_app, :my_site, mode: :testing

See Beacon.start_link/1 for more info on how to setup your Beacon configuration.

Then use this module either in your test module or in your test case template:

defmodule MyAppWeb.CMSTest do
  use MyAppWeb.ConnCase
  use Beacon.Test
  # ...
end

or make it available for all your tests by adding it to your test case template:

defmodule MyAppWeb.ConnCase do
  use ExUnit.CaseTemplate

  using do
    quote do
      use Beacon.Test
      # ...
    end
  end
end

With this configuration, Beacon will behave in a way that is better suited for testing:

  • Do not hot-load resources during boot
  • Do not broadcast events on Content changes
  • Perform most operations in a synchronous way
  • Reload module as fixture data is created

And all functions in Beacon.Test.Fixtures will be imported to help you create resources in your tests.

Default site

Most of the functions need a site option to know which site to operate on. If you don't provide it, the default site :my_site is used:

create_page_fixture(title: "Home")
%Beacon.Content.Page{site: :my_site, title: "Home"}

Or you can override it:

create_page_fixture(site: :blog, title: "Home")
%Beacon.Content.Page{site: :blog, title: "Home"}

But doing so every time is not efficient, so you can set a default site that will be used in all function calls:

use Beacon.Test, site: :blog

create_page_fixture(title: "Home")
%Beacon.Content.Page{site: :blog, title: "Home"}

Summary

Functions

Returns the site defined in the current test.

Functions

@spec default_site() :: atom()

Returns the site defined in the current test.

Default is :my_site if the option :site was not set in use Beacon.Test