AshScenario.Scenario (ash_scenario v0.6.0)
View SourceTest scenario DSL for creating named data setups with overrides.
This module provides both a macro-based DSL for defining named scenarios in test modules and direct functions for executing prototypes with different strategies.
Direct Execution
Execute prototypes directly without named scenarios:
# Create resources in the database (default)
{:ok, resources} = AshScenario.run([
{Post, :published_post},
{User, :admin}
])
# Create in-memory structs without persistence
{:ok, structs} = AshScenario.run([
{Post, :published_post}
], strategy: :struct)Named Scenarios
Define reusable scenarios in test modules:
defmodule MyTest do
use ExUnit.Case
use AshScenario.Scenario
scenario :basic_setup do
another_post do
title "Custom title for this test"
end
end
test "basic scenario" do
{:ok, instances} = AshScenario.run_scenario(__MODULE__, :basic_setup)
assert instances.another_post.title == "Custom title for this test"
end
endOptions
:extensions(list of module that adoptsSpark.Dsl.Extension) - A list of DSL extensions to add to theSpark.Dsl:otp_app(atom/0) - The otp_app to use for any application configurable options:fragments(list ofmodule/0) - Fragments to include in theSpark.Dsl. See the fragments guide for more.
Summary
Functions
Execute prototypes with the specified strategy.
Execute all prototypes defined for a resource module.
Run a named scenario from a test module.
Types
@type created_resources_map() :: %{required(prototype_ref()) => struct()}
Functions
Execute prototypes with the specified strategy.
Parameters
prototype_refs- List of prototype references as{Module, :prototype_name}tuplesopts- Options for execution
Options
:strategy- Execution strategy (:databaseor:struct, defaults to:database):domain- The Ash domain to use (will be inferred if not provided):overrides- Map of attribute overrides keyed by prototype reference
Examples
# Execute with database persistence (default)
{:ok, resources} = AshScenario.run([
{User, :admin},
{Post, :published_post}
])
# Execute as in-memory structs
{:ok, structs} = AshScenario.run([
{User, :admin}
], strategy: :struct)
# With overrides
{:ok, resources} = AshScenario.run([
{Post, :draft}
], overrides: %{{Post, :draft} => %{title: "Custom Title"}})
Execute all prototypes defined for a resource module.
Parameters
resource_module- The Ash resource module containing prototype definitionsopts- Options for execution (same asrun/2)
Examples
{:ok, resources} = AshScenario.run_all(Post)
{:ok, structs} = AshScenario.run_all(Post, strategy: :struct)
Run a named scenario from a test module.
This function is for compatibility with the scenario DSL.
Options
:domain- The Ash domain to use (will be inferred if not provided):strategy- Execution strategy (:databaseor:struct, defaults to:database)
Examples
{:ok, instances} = AshScenario.run_scenario(MyTest, :basic_setup)
{:ok, instances} = AshScenario.run_scenario(MyTest, :basic_setup, domain: MyApp.Domain)
{:ok, structs} = AshScenario.run_scenario(MyTest, :basic_setup, strategy: :struct)