docs » cp.spec.Scenario
A Definition which describes a specific scenario.
A Scenario
is most typically created via the it function, like so:
local spec = require "cp.spec"
local describe, it = spec.describe, spec.it
local Rainbow = require "my.rainbow"
return describe "a rainbow" {
it "has seven colors"
:doing(function()
local rainbow = Rainbow()
assert(#rainbow:colors() == 7, "the rainbow has seven colors")
end)
}
Scenarios can be run asynchronously via the Run.This instance passed to the doing
function.
To indicate a scenario is asynchronous, call this:wait()
, then call
this:done()
, to indicate it has completed. Any assert
call which fails will
result in the run failing, and stop at that point.
For example:
return describe "a rainbow" {
it "has a pot of gold at the end"
:doing(function(this)
this:wait()
local rainbow = Rainbow()
rainbow:goToEnd(function(whatIsThere)
assert(whatIsThere:isInstanceOf(PotOfGold))
this:done()
end)
end)
}
Definitions can also be data-driven, via the where method:
return describe "a rainbow" {
it "has ${color} at index ${index}"
:doing(function(this)
local rainbow = Rainbow()
assert(rainbow[this.index] == this.color)
end)
:where {
{ "index", "color" },
{ 1, "red" },
{ 2, "orange" },
{ 3, "yellow" },
{ 4, "blue" },
{ 5, "green" },
{ 6, "indigo" },
{ 7, "violet" },
},
}
This will do a run for each variation and interpolate the value into the run name for each.
Note: "where" parameters will not override built-in functions and fields in the this instance (such as "async" or "done") so ensure that you pick names that don't clash.
- Constructors - API calls which return an object, typically one that offers API methods
- Scenario
- Methods - API calls which can only be made on an object returned by a constructor
- doing
- run
- where
Signature | cp.spec.Scenario(name[, testFn]) -> cp.spec.Scenario |
---|---|
Type | Constructor |
Description | Creates a new Scenario with the specified name. |
Parameters |
|
Returns |
|
Notes |
|
Signature | cp.spec.Scenario:doing(actionFn) -> self |
---|---|
Type | Method |
Description | Specifies the function for the definition. |
Parameters |
|
Returns |
|
Signature | cp.spec.Scenario:run(...) -> cp.spec.Run |
---|---|
Type | Method |
Description | Runs the scenario. |
Parameters |
|
Signature | cp.spec.Scenario:where(data) -> cp.spec.Where |
---|---|
Type | Method |
Description | Specifies a table of data that will be iterated through as multiple Runs, one row at a time. |
Parameters |
|
Returns |
|