question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose story context to the success result of the story run method.

See original GitHub issue

Stories can be used both as stand-alone objects or substories of other stories.

If we want to return a value from such a story when we run it as a stand-alone object, we have a problem.

We can’t use the Result class in the story since it’ll stop the execution of the parent story when we inject the story.

For example, we have a story like this:

class TokenUseCase:
    @story
    @arguments('user')
    def obtain_token(I):
        I.require_user_active
        I.create_token
        I.return_token

    def require_user_active(self, ctx):
        if ctx.user.is_active:
            return Success()
        return Failure()

    def create_token(self, ctx):
        ctx.token = self.create_user_token(ctx.user)
        return Success()

    def return_token(self, ctx):
        return Result({'token': ctx.token})

In this case, we have two options:

  1. Split this story into two parts. One will be used as a substory. The other one is the stand-alone wrapper from two steps. One for the story, one for the return statement.
  2. Pass a boolean somehow (IoC for example) to the last step to decide what kind of objects we need to return.

Both variants do not bring me joy.

To be clear, in most cases Result won’t be a problem If we describe the actual process from the business domain, and this process supposed to give us a result, this will be a valid behavior on any level of nesting.

The problem appears only for relatively low-level stuff like tokens. We can’t make any business decision at this level of logic, so Result is not suited there by design.

Even it’s a relatively rare problem from a business logic perspective, I don’t want to force users to write wrappers and pass boolean.

return_token should not even exist in this case.

I propose to make available ctx object on the successful result of the run method.

This way we’ll be able to access the token variable when we want to use it as a stand-alone story.

class TokenUseCase:
    @story
    @arguments('user')
    def obtain_token(I):
        I.require_user_active
        I.create_token

    def require_user_active(self, ctx):
        if ctx.user.is_active:
            return Success()
        return Failure()

    def create_token(self, ctx):
        ctx.token = self.create_user_token(ctx.user)
        return Success()

result = TokenUseCase().obtain_token.run(User())
assert result.is_success
assert result.ctx.token == 'abc'

I would like to invite @ditansu and @ramidk to discuss this proposal.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
proofit404commented, Apr 15, 2020

@ditansu From what I understand, you’ll be happy with the origin proposal from this issue.

0reactions
proofit404commented, Mar 23, 2021

Superseded by #627

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Develop a Success Story - CDC
A success story can document program improvement over time and demonstrate the value of program activities. When presented effectively, success stories can be...
Read more >
What's Your Story? - Harvard Business Review
Without a story, there was no context to render career facts meaningful, no promise of a third act in which achieving a goal...
Read more >
How to Write a User Story for an API Product
Therefore, you DO have to care about the experience of the developer, their context, to have a successful API product. Structuring the Stories....
Read more >
Definition and Overview of Story Mapping Prioritization
Story mapping is a method for arranging user stories to create a more holistic view of how they fit into the overall user...
Read more >
10 Tips for Writing Good User Stories - Roman Pichler
Keep them simple and concise. Avoid confusing and ambiguous terms, and use active voice.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found