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

Stay in Current State for specified number of requests #494

Closed
shoaibshakeel381 opened this issue Jul 30, 2020 · 5 comments
Closed

Stay in Current State for specified number of requests #494

shoaibshakeel381 opened this issue Jul 30, 2020 · 5 comments
Labels

Comments

@shoaibshakeel381
Copy link

shoaibshakeel381 commented Jul 30, 2020

Hey guy,s I need to write mocks such that a request keeps faulting for a given number of time before it can succeed. I know that I can use Scenarios for this.

But there's a problem. Let's suppose I want to fault my request 2 times before succeeding on 3rd attempt. So I have to write three mocks where flow would be like this:

-- 1st Mock:
    -- Set next state to "fail"
    -- Fail request

-- 2nd Mock:
    -- When current state is "fail"
    -- Set next state to "pass"
    -- Fail request

-- 3rd Mock:
    -- When current state is "pass"
    -- Pass request

We can see that first 2 states are just doing same thing. So my question is this: Is it possible to build a mock which stay in its current state for a given number of time, before switching to next one. So my mocks would be like below:

-- 1st Mock:
    -- Set next state to "pass" ONLY after 2 requests
    -- Fail request

-- 2nd Mock:
    -- When current state is "pass"
    -- Pass request
@StefH
Copy link
Collaborator

StefH commented Jul 30, 2020

@shoaibshakeel381

Probably the interface can look like:

var server = WireMockServer.Start();

server
                .Given(Request.Create().WithPath(path).UsingGet())
                .InScenario("S")
                .WillSetStateTo("bar")
                .WhenMatchingCountEquals(2)
                .RespondWith(Response.Create().WithBody("Scenario S, Setting State bar"));

server
                .Given(Request.Create().WithPath(path).UsingGet())
                .InScenario("S")
                .WhenStateIs("bar")
                .RespondWith(Response.Create().WithBody("Scenario S, State bar"));

or

var server = WireMockServer.Start();

server
                .Given(Request.Create().WithPath(path).UsingGet())
                .InScenario("S")
                .WillSetStateTo("bar", 2) // 2 = matching count
                .RespondWith(Response.Create().WithBody("Scenario S, Setting State bar"));

server
                .Given(Request.Create().WithPath(path).UsingGet())
                .InScenario("S")
                .WhenStateIs("bar")
                .RespondWith(Response.Create().WithBody("Scenario S, State bar"));

@shoaibshakeel381
Copy link
Author

@StefH I would suggest second approach. Here's why:

  • This count is highly related to WillSetStateTo() so it would make sense to keep both values in one atomic operation
  • WhenMatchingCountEquals() can be misplaced like this:
server
                .Given(Request.Create().WithPath(path).UsingGet())
                .WhenMatchingCountEquals(2)
                .InScenario("S")
                .WillSetStateTo("bar")
                .RespondWith(Response.Create().WithBody("Scenario S, Setting State bar"));

which is not very readable. It can even be used like this:

server
                .Given(Request.Create().WithPath(path).UsingGet())
                .WhenMatchingCountEquals(2)
                .RespondWith(Response.Create().WithBody("Scenario S, Setting State bar"));

which doesn't make sense.

@StefH
Copy link
Collaborator

StefH commented Aug 1, 2020

@shoaibshakeel381
Can you try preview version WireMock.Net.1.2.16-ci-13650.nupkg from MyGet ? (https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)

@shoaibshakeel381
Copy link
Author

I tested this preview version on my tests and it worked flawlessly. Great job.

@StefH StefH added feature and removed question labels Aug 1, 2020
@StefH
Copy link
Collaborator

StefH commented Aug 1, 2020

I'll create an official version within some time.

#495

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants