danawoodman / sinon-express-mock

Simple request and response mock objects to pass into Express routes when testing using Sinon.

Home Page:https://www.npmjs.com/package/sinon-express-mock

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do you do POST request mocks?

kbirger opened this issue · comments

I have tried moqReq({ method: 'POST'}), but no luck. I don't see anything for it in the code either.

What are you attempting to do? We take anything from the passed in object and return it along with some request method stubs. Also, it is mockReq btw 👍

Please reopen if you think something is broken in the module 👍

Thanks @danawoodman, I do not think this necessarily anything broken with the module, but I don't really know another place to ask, plus, if it turns out to be functionality not supported, there's history for a feature request. You can decide :).

I'm moving from a package named mock-express-request, and I have a piece of middleware I'm testing. The middleware needs to process requests of different types (GET/POST/etc), and its behavior is different based on the type of request. In the other package, I was able to do something like mockRequest({ method: 'POST'}), to test my middleware's condition:

middleware(req: express.Request, res: express.Response, ...) {
  if (req.method === 'POST') {
    doSomething(); 
  } else { 
    doSomethingElse();
}

Can I perform an analogous operation with this package?

Thanks again!

You should be able to. Have you tried? If you pass {method: 'POST'} and then pass the request object to a method, it will be able to operate on it like normal. Give it a shot and let me know!