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.

Spy or stub methods before mount

See original GitHub issue

I’ve got some methods that are called on created, mounted events. I can’t seem to be able to spy on them properly (or make stubs).

What I tried so far is, is spying on methods of pre mounted element and then checking if the spy was called. This doesn’t work even when the actual method was called (tested by adding simple consolel.og)

Example:

it('calls myMethod when mounted', () => {
  const spy = sinon.spy(myComponent.methods, 'myMethod')
  const wrapper = mount(myComponent, { propsData, store })
  expect(spy.called).to.be.true // AssertionError: expected false to be true
})

Using Karma, Mocha and Sinon. I hope this is enough info and appropriate place to ask this.

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
eddyerburghcommented, Apr 24, 2017

This test works for me -

it('calls changeMessage on mount', () => {
  const spy = sinon.spy(Foo.methods, 'focusOnComposer')
  mount(Foo)
  expect(spy.called).to.be.true
});

There must be something else going on here.

Can you link me to the full repo?

2reactions
keligijuscommented, May 9, 2017

Edd, you’re a legend man‼️

Thanks so much, now the joy of coding is back 🕺

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stubs, Spies, and Clocks | Cypress Documentation
The same concept applies to mounting a component with cy.mount() . We bind before any timers from your code can be invoked. Restore...
Read more >
java - Mockito - @Spy vs @Mock - Stack Overflow
A spy in mockito is a partial mock in other mocking frameworks (part of the object ... If you want to change and...
Read more >
Best Practices for Spies, Stubs and Mocks in Sinon.js
The assertion within the stub ensures the value is set correctly before the stubbed function is called. Remember to also include a sinon.assert....
Read more >
Stubs, Spies and Mocks in JavaScript · Harry's Blog
Spy on functions and see when or if they are called. Pros: Check if a method has been called with certain variables; Check...
Read more >
Mock vs Stub vs Spy - Javatpoint
In Mockito, spy() method is used for creating spy objects. It allows us to call the normal methods of the real object. The...
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