Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetError to context #55

Closed
sagikazarmark opened this issue Jan 18, 2020 · 5 comments · Fixed by #69
Closed

Add GetError to context #55

sagikazarmark opened this issue Jan 18, 2020 · 5 comments · Fixed by #69
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@sagikazarmark
Copy link
Collaborator

sagikazarmark commented Jan 18, 2020

Is your feature request related to a problem? Please describe.
When testing a service, I often want to define business rules for happy and unhappy paths.

For example, when creating a new resource I want to test that certain invalid conditions result in an appropriate response.

Creating the resource creation itself might be described by the same step definition as the happy path.

For example:

Scenario: Add a new item to the list
    Given there is an empty todo list
    When I add entry "Call mom"
    Then I should have a todo to "Call mom"

Scenario: Cannot add an empty todo item
    Given there is an empty todo list
    When I add entry ""
    Then I should see validation error

In this case, When I add entry "" should result in calling the service in both cases.

In the invalid case I want to check the returned error, in the valid case I want the test to fail if an error is returned.

A possible solution is passing the error to the context and checking it's value in a then step. Unfortunately, there is no GetError.

Describe the solution you'd like
Add GetError to the context that returns the value as an error.

Describe alternatives you've considered
I'm not sure, is there a better way to conditionally test error paths?

At the moment I'm adding this to every then step:

		if err := ctx.Get("error", nil); err != nil {
			return err.(error)
		}

Maybe have builtin support for errors in the context? Chances are if a service call fails and an error is set in the context that particular step returns.

The error in the context could even cause the test to fail if it's not "handled" (eg. retrieved by a subsequent step).

@sagikazarmark sagikazarmark added the enhancement New feature or request label Jan 18, 2020
@bkielbasa
Copy link
Collaborator

I'm not sure about that. Firstly, please notice that we use context package so we cannot just add a method to it.

In such cases, I was thinking about using custom helper functions like

func getError(ctx context.Context) error {
    if err, ok := ctx.Value("error"); ok {
      return err
    }

    return nil
}

@bkielbasa
Copy link
Collaborator

I'm still not convinced about the change. You can store the error as a regular value.

@sagikazarmark
Copy link
Collaborator Author

True, but it's a very typical use case (call a service, check if you receive an error), so I think it would make sense to make that easier somehow.

It would even be okay to have a helper function, that retrieves an error from the context.

My goal is to make the test implementation easier to read.

@bkielbasa
Copy link
Collaborator

What I can suggest adding is something like this:

if err := ctx.GetError("error"); err != nil {
    return err
}

WDYT? So it would work the same way tha GetInt etc works.

@sagikazarmark
Copy link
Collaborator Author

That's my original proposal 🙂

@bkielbasa bkielbasa added this to the 1.0 milestone Feb 11, 2020
@bkielbasa bkielbasa self-assigned this Feb 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants