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.

Discussion: Best API for stubbing methods?

See original GitHub issue

Currently, 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:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
eddyerburghcommented, Sep 29, 2017

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

1reaction
heygambocommented, Jan 6, 2018

setMethods worked great.

like here:

const spy = jest.spyOn(wrapper.vm, 'handleChange')
wrapper.setMethods({ handleChange: spy })
wrapper.find('.some-input').trigger('change')
expect(spy).toHaveBeenCalled()
Read more comments on GitHub >

github_iconTop 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 >

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