-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STORY : Added failing test for rewrite given feature.
- Loading branch information
1 parent
edcfa7e
commit 2e07cd2
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
Story that rewrites given preconditions: | ||
docs: engine/rewrite-given | ||
about: | | ||
These examples show how to build stories that rewrite themselves | ||
from program output (in-test snapshot testing) but that rewrite | ||
the given preconditions. | ||
This is useful for changing | ||
``` | ||
self.current_step.rewrite("argument").to("new output") | ||
``` | ||
given: | ||
files: | ||
example.story: | | ||
Call API: | ||
given: | ||
mock api: | ||
request: | | ||
{"greeting": "hello"} | ||
response: | | ||
{"greeting": "hi"} | ||
steps: | ||
- Call API | ||
engine.py: | | ||
from hitchstory import BaseEngine, GivenDefinition, GivenProperty | ||
from strictyaml import Map, Str | ||
class Engine(BaseEngine): | ||
given_definition = GivenDefinition( | ||
mock_api=GivenProperty( | ||
schema=Map({"request": Str(), "response": Str()}), | ||
inherit_via=GivenProperty.OVERRIDE, | ||
), | ||
) | ||
def __init__(self, rewrite=True): | ||
self._rewrite = rewrite | ||
def call_api(self): | ||
if self._rewrite: | ||
self.given.rewrite("Mock API", "response").to("""{"greeting": "bye"}""") | ||
setup: | | ||
from hitchstory import StoryCollection | ||
from pathlib import Path | ||
from engine import Engine | ||
steps: | ||
- Run: | ||
code: | | ||
StoryCollection(Path(".").glob("*.story"), Engine(rewrite=True)).ordered_by_name().play() | ||
will output: |- | ||
RUNNING Call API in /path/to/working/example.story ... SUCCESS in 0.1 seconds. | ||
- File contents will be: | ||
filename: example.story | ||
contents: |- | ||
Call API: | ||
given: | ||
mock api: | ||
request: | | ||
{"greeting": "hello"} | ||
response: | | ||
{"greeting": "bye"} | ||
steps: | ||
- Call API |