cabbage-ex / cabbage

Story BDD tool for executing elixir in ExUnit
MIT License
138 stars 33 forks source link

Testing controllers on a Phoenix App #82

Closed zemuldo closed 5 years ago

zemuldo commented 5 years ago

Hi, let me first say thanks for creating this tool. I am trying to write a test for a BDD feature while the feature file is below:

Feature: Root path must always be healthy with 200 on GET
  API should always be online
  Scenario: Visit Index Page
    Given I send a GET requested to root path
    Then I should get 200

The feature file is for a root path of an HTTP endpoint of a Phoenix App. The test I have written is as below:

defmodule MyAppWeb.PageControllerTest do
  use MyAppWeb.ConnCase
  # Options, other than file:, are passed directly to `ExUnit`
  use Cabbage.Feature, async: false, file: "page_controller_index.feature"
  alias Gherkin.Elements.Scenario

  test "has a @feature" do
    assert "Root path must always be healthy with 200 on GET" = @feature.name
  end

  setup do
    {:ok, %{}}
  end
  defgiven ~r/^I send a GET requested to root path$/, _,_ do
    conn = get(conn, "/")
    {:ok, %{conn: conn}}
  end

  defthen ~r/^I should get 200$/, _, %{conn: conn} do
    assert html_response(conn, 200) =~ "Welcome to Phoenixxx!"
  end
end

PROBLEM: When I run mix test the test runs and skips my given and then clauses. The test has a @feature however runs and passes. I expect the then to fail but the test never gets there! It might be my setup but I have followed the README and compared. I cant get it to work.

zemuldo commented 5 years ago

I realized I was doing endpoint tests so instead of use MyAppWeb.ConnCase I used Phoenix.ConnTest to provide the functions required.