跳到内容

这展示了如何构建一个重写参数子键的故事。

self.current_step.rewrite("response", "content").to("new output")

代码示例

example.story

REST API:
  steps:
  - API call:
      request:
        path: /hello
      response:
        status code: 200
        content: |
          {"old": "response"}
engine.py

from hitchstory import BaseEngine

class Engine(BaseEngine):
    def __init__(self, rewrite=True):
        self._rewrite = rewrite

    def run(self, command):
        pass

    def api_call(self, request, response):
        if self._rewrite:
            self.current_step.rewrite(
                "response", "content"
            ).to("""{"new": "output"}""")

带有代码

from hitchstory import StoryCollection
from pathlib import Path
from engine import Engine

当使用 rewrite=True 时,故事被重写

StoryCollection(Path(".").glob("*.story"), Engine(rewrite=True)).ordered_by_name().play()

将输出

RUNNING REST API in /path/to/working/example.story ... SUCCESS in 0.1 seconds.

文件 example.story 现在应该包含

REST API:
  steps:
  - API call:
      request:
        path: /hello
      response:
        status code: 200
        content: |-
          {"new": "output"}

当使用 rewrite=False 时,故事保持不变

StoryCollection(Path(".").glob("*.story"), Engine(rewrite=False)).ordered_by_name().play()

将输出

RUNNING REST API in /path/to/working/example.story ... SUCCESS in 0.1 seconds.

然后示例故事将保持不变。

可执行规范

rewrite-subkey-of-argument.story storytests 自动生成的文档。