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

Supporting different states of a mocked response #52

Open
AyaAkl25 opened this issue Aug 15, 2019 · 2 comments
Open

Supporting different states of a mocked response #52

AyaAkl25 opened this issue Aug 15, 2019 · 2 comments

Comments

@AyaAkl25
Copy link

Hey guys,

just wondering because I didn't find a doc about this, but what we have a case of doing a mock to different states of the same response.
for example:
/welcome api will return "Hi World" and then on subsequent /welcome needs to be returned like “Hi Globe”

is there a way to play around this that could enable me to mock the response in states
because my issue now is that the UI test just continue without waiting the response to be fully mocked (same response) or it just override everything to the latest mock

Thanks so much guys for your support that I really need because I am stuck in this for quite some time :)

@fangpenlin
Copy link
Contributor

@Aya-Akl

You can use define a state class and pass the reference into your response handler closure. Modify the value of State every time it gets called. Here's an example

class State {
    var index = 0
}
let state = State()
let responses: [String] = ["Hi World", "Hi Globe", "Hola", "What's up"]

router["/responses"] = DelayResponse(JSONResponse(handler: { [state, responses] _ -> Any in
    let index = state.index
    state.index += 1
    state.index = state.index % responses.count
    return [
        "value": responses[state.index]
    ]
}))

@AyaAkl25
Copy link
Author

@fangpenlin Thank you so much for this, it helped me so much adding my data responses and passing them in states.

there is some concern left, when I did this, sometimes the responses got mixed so the API is not mocked in the same order of state I am given, I had to use sleep(timeout) which I don't like, is there any reason might be the reason for this issue?

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

No branches or pull requests

2 participants