question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing error handling?

See original GitHub issue

This just a question, not a real issue, but in your section on Error Handling you have the following example:

function* checkout(getState) {

  while( yield take(types.CHECKOUT_REQUEST) ) {
    try {
      const cart = getState().cart
      yield call(api.buyProducts, cart)
      yield put(actions.checkoutSuccess(cart))
    } catch(error) {
      yield put(actions.checkoutFailure(error))
    }
  }
}

How would I go about testing the error case? I’d like to test what happens when the function passed in the call statement rejects its Promise or throws an Error.

The best thing I can think of is trying to mock the call function to just throw an error, but that seems messier than I’d like. Any ideas? Thanks!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:10
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

114reactions
yelouaficommented, Jan 6, 2016

The best thing I can think of is trying to mock the call function to just throw an error

You don’t have to mock, yield call(...) doesn’t execute anything, to test the generator you provide it with dummy responses. To simulate a promise rejection, call throw on the iterator

const cart = {}
const getState = () => ({cart})
const iterator = checkout(getState)

assert.deepEqual(iterator.next().value, take(types.CHECKOUT_REQUEST))
assert.deepEqual(iterator.next(actions.checkoutRequest()).value, call(api.buyProducts, cart) )
// simulate a promise rejection
assert.deepEqual(iterator.throw('error').value, put(actions.checkoutFailure('error')))
16reactions
KamuelaFrancocommented, Nov 21, 2016

Can this go in the docs? Please?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling Testing - H2k Infosys
Error handling testing is a type of software testing that is performed to check whether the system is capable of or able to...
Read more >
Testing Error Handling | Shakacode
You can simulate error conditions by manually placing =raise "any error message"= statements in your code, and then testing, say in the UI ......
Read more >
Testing for Improper Error Handling - OWASP Foundation
Analyse the expected input type (strings, integers, JSON, XML, etc.). Fuzz every input point based on the previous steps to have a more...
Read more >
What is Error Handling Testing? (Software Testing Interview ...
In this session, I have answered What is Error Handling Testing ?
Read more >
Software Testing Errors to look out for (with examples)
Programmers usually pass an 'Error Flag' in the function to keep track of the success or failure of the function. It is advisable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found