Discussion: Best API for stubbing methods?
See original GitHub issueCurrently, to stub methods on a component you need to do this:
const wrapper = mount(TestComponent)
wrapper.vm.clickHandler = sinon.stub() // Stub the method
wrapper.update() // Force the vm to update
wrapper.find('button').trigger('click')
expect(wrapper.vm.clickHandler.called).to.equal(true)
I think we should either add a method, or an option that stubs component methods:
// Method
const wrapper = mount(TestComponent)
wrapper.stubMethod(clickHandler, sinon.stub()) // Stubs the method and forces update
wrapper.find('button').trigger('click')
expect(wrapper.vm.clickHandler.called).to.equal(true)
It could also be called setMethods
, like the setData
and setProps
methods, and take an object.
// Option
const wrapper = mount(TestComponent, {
stubMethods: { // stubs methods before mount
clickHandler: clickHandlerStub
}
})
wrapper.find('button').trigger('click')
expect(wrapper.vm.clickHandler.called).to.equal(true)
Personally I prefer the stubMethod
/ setMethods
approach, but I’d like to hear peoples thoughts 🙂
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
API Mocking Tools and Best Practices
In this article we will look at some of the most popular API mocking tools and frameworks. We will also go through the...
Read more >Stubbing, Mocking and Service Virtualization Differences ...
The intent is to provide an understanding of all three techniques that enables to you to choose the best option between a mock,...
Read more >Stubbing HTTP Requests with Sinon
Describe what a stub is and why you would want to use them in your test suites; Discuss the benefits of using Sinon...
Read more >How to test software: mocking, stubbing, and contract testing
We'll cover the techniques of mocking and stubbing, and test-driven development to help each testing layer. First, let's review a concept ...
Read more >how to test the Rest Callout using Stub API
The StubProvider interface is not the correct tool to use in a unit test involving a callout. This interface literally overrides the method...
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 FreeTop 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
Top GitHub Comments
There’s now a
setMethods
method.We should have a setComputed method too. I’ve created a new issue for that:
https://github.com/vuejs/vue-test-utils/issues/55
setMethods
worked great.like here: