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.

Unit Testing and mocking

See original GitHub issue
  • Node Version: v8.9.1
  • Npm Version: 5.5.1
  • Platform: Darwin Kernel Version 17.2.0
  • Package Version: 4.6.4

I am trying to write a unit test for an action that uses the createClient API. I’ve attempted to intercept the URL that it should be hitting, but to no avail. I am seeking help on the best way to test this by intercepting the request to Contentful. Below is a bit of the code:

actions.js

meow = () => {
  mergedParams = {
    limit: 100,
    'content_type': 'XXXXX',
  }
  return client.getEntries(mergedParams)
    .then((response) => {
      response = JSON.parse(response.stringifySafe())
      dispatch(returnAuthorFetchSuccess(response.items))
    })
    .catch((response) => {
      dispatch(returnAuthorFetchFailure({
        error: 'Unable to fetch XXXXX.',
        response,
      }))
    })
}

test.js

fetchMock.getOnce(`https://cdn.contentful.com:443/spaces/${process.env.REACT_APP_CONTENTFUL_SPACE}/entries?access_token=${process.env.REACT_APP_CONTENTFUL_TOKEN}&content_type=XXXXX`, { value: 'meow' })

    return store.dispatch(actions.meow())
      .then(() => {
        const response = store.getActions()
     })

Does anyone have any insight on the best way to approach this?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Khaledgarbayacommented, Nov 29, 2017

Hey @mbenjamin618 , There is different ways of mocking the library depends on your use case. there is a library called contentfaux that maybe can help you.

0reactions
ChazUKcommented, Nov 6, 2022

@matthewfbenjamin any example of this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

unit testing - What is Mocking? - Stack Overflow
Mocking is primarily used in unit testing. An object under test may have dependencies on other (complex) objects. To isolate the behaviour ...
Read more >
Mocking Framework for Unit Testing - Telerik JustMock
Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate...
Read more >
Mocking in Unit Tests - Code With Engineering Playbook
The main difference between a mock and most of the other test doubles is that mocks do behavioral verification, whereas other test doubles...
Read more >
Unit Testing with Mocking in 10 Minutes | by Kay Jan Wong
Unit Testing with Mocking in 10 Minutes. Test your codebase effectively with the built-in unittest Python package.
Read more >
unittest.mock — mock object library — Python 3.11.1 ...
unittest.mock is a library for testing in Python. It allows you to replace parts of your system under test with mock objects and...
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