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:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
@matthewfbenjamin any example of this?