code-corps / code-corps/stripity_stripe_testing

RFC for testing package design

Open
#1 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Elixir
Stars
1
Forks
0
PR merge metrics
No merged PRs in 30d

Description

We should discuss what this should look like.

Here are some notes from https://github.com/code-corps/stripity-stripe/issues/124

The test package can distribute a lightweight replica of the Stripe service. That way, testing can be done something like the following:

```elixir
test "retrieves all plans where the storage limit is greater than 30" do
Stripe.Test.create(:plan, 10)
Stripe.Test.create(:plan, 10, metadata: [storage_limit: 10])
Stripe.Test.create(:plan, 10, metadata: [storage_limit: 35])
Stripe.Test.create(:plan, 10, metadata: [storage_limit: 30])

# This is business logic that will call the StripityStripe module
plans = MyApp.Module.get_plans(:gt, 30)

assert Enum.count(plans) == 10
end
```

The data can be stored on a per-test basis in ETS, allowing users to build up the necessary state and tear it down without needing to know the intricacies of the system.

For example:

```elixir
defmodule Stripe.CustomerTest do
# this may be able to be true
use ExUnit.Case, async: false

# Most of this setup process can be abstracted into the library, but putting
# it here so that others understand
setup do
# Starts a separate process
# - That process loads a Plug responder that binds to an available port
# - It also seeds an ETS table that it claims ownership of
# - The ETS table and Plug responder are bound to this specific test,
# so they are isolated to this test and any state manipulation on them
# will not affect other tests
# - Returns a struct of some form like %Stripe.Test.Server{port: 4515, pid: #PID<0.35.0>}
{:ok, stripe_server} = Stripe.Test.Server.start()

Application.put_env(:stripity_stripe, :api_base_url, "http://localhost:#{stripe_server.port}")

on_exit fn ->
# Cleans up the ETS table and stops the plug responder and owning process
Stripe.Test.Server.stop(stripe_server)
end

{:ok, stripe: stripe_server}
end

describe "Stripe.Customer.retrieve/1" do
test "retrieves a customer by ID", %{stripe: stripe} do
# Uses the Stripe.Test.Customer module to create a new customer in the
# ETS backed server (essentially, this is a factory method)
%{id: id} = Stripe.Test.Customer.create(stripe)

# Makes an actual API request from the high level API through the low level
# API and through the OS network stack
{:ok, customer} = Stripe.Customer.retrieve(id)

assert customer.id == id
end
end
end
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.