apocryphal icon indicating copy to clipboard operation
apocryphal copied to clipboard

DSL vs generating ExUnit directly

Open coryodaniel opened this issue 8 years ago • 4 comments

test "[GET] /kittens (200)", %{swagger: swagger} do
  my_kitten_factory(10)

  swagger
    |> Transaction.build(@mime, :get, "/kittens", 200)
    |> assert_schema 
end

test "Create a kitten", %{swagger: swagger} do
  swagger
    |> Transaction.build(@mime, :post, "/kittens", 201)
    |> put_in([:request, :body], %{name: "Chuancy", type: "cat"})
    |> assert_schema
end

test "Strict kitten making", %{swagger: swagger} do
  swagger
    |> Transaction.build(@mime, :post, "/kittens", 201)
    |> put_in([:request, :body], %{name: "Chuancy", type: "cat"})
    |> assert_match except: [:inserted_at, :updated_at]
end

coryodaniel avatar Sep 27 '16 01:09 coryodaniel

FWIW, it would be nice if one could approach a flow like this:

  1. Edit pet_store.yml file to add a new API feature
  2. Run 'mix test' and see a new test that fails
  3. Implement code to handle the failing test

I suppose that compiling the spec into tests could be handled by the file watcher if you use Phoenix, but you might just as well be doing "clean" Plug code if you're developing an API :)

How about having an interface similar to ExUnit.DocTest, where you just use a macro to point to the .yml source, and the tests will be generated at compile time when you run mix test?

jwarlander avatar Sep 27 '16 07:09 jwarlander

I had originally done a DSL (https://github.com/coryodaniel/apocryphal/tree/sly-dsl) and then tore it all up and went with bare ExUnit because it seems more clear (less macros)... Want to peek at the DSL branch and let me know what you think?

coryodaniel avatar Sep 27 '16 16:09 coryodaniel

Well, for me personally.. I'd just rather not see, or have, any code in my code base that's just generated strictly off of a spec.. The spec is the test code in this case, just like the docs are the tests for DocTests; they don't generate code that's sitting around in your repo.

I don't know where you are on this, though.. it's perfectly fine to have a different opinion, of course ;)

At work we're looking at possibly implementing a couple of small, internal APIs using Elixir. I'd love to play around with running Swagger specs as "acceptance tests" for these things. So I may have more specific ideas / suggestions / questions when we get to that :)

jwarlander avatar Sep 28 '16 13:09 jwarlander

Note, of the two options.. DSL vs. plain ExUnit.. I do like the plain ExUnit version better :)

jwarlander avatar Sep 28 '16 13:09 jwarlander