Skip to content

Instantly share code, notes, and snippets.

@khanghoang
Last active January 11, 2016 08:55
Show Gist options
  • Save khanghoang/267d834cbed479472a3b to your computer and use it in GitHub Desktop.
Save khanghoang/267d834cbed479472a3b to your computer and use it in GitHub Desktop.
Mock

Mock

Why you don't use ZhenLing mock server?

  • We still use it, but not all the dev time.
  • Every time we want to code, test we need to open simulator, so it will be hard for us if we want to use any automation tests later on.
  • if (env === "DEV") {} is everywhere, we need to do it just in one place.

New way to mock

Mock

Extra benefits

  • Dev cans change the response whenever they want, we can do even better than that for example we can set the the loading time or set the request to false.

How

  • We gonna implement something likes this javascript const mock = (req) => { req.prototype.send = (successBlock, failureBlock) => { successBlock(window.btoa(writeBinaryString(loginForm))); }

    return req; }

- We also need to predefine the responses if we gonna call with correct url/parameters (the idea like **[Nock](https://github.com/pgte/nock)**).
- Then wherever can wrap the global `Request` with `mock(Request)`.
@penmanglewood
Copy link

PLEASE PLEASE PLEASE PLEASE do not put if (env === "DEV") {} everywhere. Abstract the data so far so that it doesn't matter where the data is coming from. You shouldn't be checking for the env type in more than two or three places in the whole project.

@khanghoang
Copy link
Author

@penmanglewood that's one of key things I wanna achieve here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment