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

How to mock reject reponse with status > 400 #6

Closed
Sutikshan opened this issue Aug 12, 2016 · 8 comments
Closed

How to mock reject reponse with status > 400 #6

Sutikshan opened this issue Aug 12, 2016 · 8 comments

Comments

@Sutikshan
Copy link

How to mock reject reponse with status > 400 .
I want to mock errorneous response i.g.

      moxios.wait(() => {
        const request = moxios.requests.mostRecent();
        request.respondWith({
          status: 422,
          response: { message: 'problem' },
        });
      });

How can I do it?

@Sutikshan
Copy link
Author

I manage to do it via following :-

      const errResp = {
        status: 422,
        response: { message: 'problem' },
      };
      moxios.wait(() => {
        const request = moxios.requests.mostRecent();
        request.reject(errResp);
      });

Thanks for this lib.

@rickhanlonii
Copy link

rickhanlonii commented Sep 14, 2016

For anyone using stubRequest you should be able to just use a non 2XX status:

moxios.stubRequest(/.*/, {
  status: 400,
  response: { message: 'problem' }
});

axios.get('something')
  .then(() => {
    // Not hit
  })
  .catch(error => {
    // Error
  });

@Sutikshan it looks like respondWith does this as well. Here it calls axois.settle which as you can see here rejects for invalid response statuses. Not sure why it wasn't working for you.

@mrchief
Copy link

mrchief commented Nov 8, 2017

@rickhanlonii request.reject produces a cleaner JSON response whereas respondWith creates a Error instance itself (or something to that effect). Its very hard to do matchers on that one.

with respondWith

Expected value to match object:
      [{"status": "started", "type": "CONFIGURED_ACCOUNTS"}, {"error": {"status": 500}, "status": "failed", "type": "CONFIGURED_ACCOUNTS"}]
    Received:
      [{"status": "started", "type": "CONFIGURED_ACCOUNTS"}, {"error": [Error: Request failed with status code 500], "status": "failed", "type": "CONFIGURED_ACCOUNTS
ACCOUNTS"}]

with reject

error { status: 500,
        response:
         { title: 'mock error',
           description: 'mock error details',
           data: undefined } }

@yonasstephen
Copy link

yonasstephen commented Nov 18, 2017

@mrchief you can use stubRequest or respondWith and still easily match the error instance as follows:

const error = new Error('Error: Request failed with status code 500')

moxios.stubRequest('/api', {
   status: 500,
   response: { error }
})

@mrchief
Copy link

mrchief commented Nov 18, 2017

Its not an issue with moxios, just a quirk of axios: axios/axios#960 (comment)

Went with the interceptor approach and now I can reliably check my asserts.

@simonsankar
Copy link

I've tried all of the proposed solutions above and in theory they should work but I'm getting 'Request failed with status code

With any successful code it works but with non-success codes it throws that error
Is there any way to test bad/failure requests?

@raulra08
Copy link

raulra08 commented Jun 4, 2020

Hi @simonsankar,
Althought it's been a while I was wondering if you were able to find a solution?
Thanks in advance

@raulra08
Copy link

raulra08 commented Jun 4, 2020

Answered on this thread

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

6 participants