Questions tagged [spy]

A spy is XUnit pattern where you replace the original implementation with a test double to capture the calls on the object. Later in your test you can verify the object under test has made specific calls on that object

425 questions
1
vote
0 answers

How to use spock @SpringSpy with a JpaRepository

I am using spring boot with jpa and spock. I want to assert that certain calls were made to the repo as a functional test, so a Mock won't do (I need to verify that a native query works with certain test data in the DB). I have tried a…
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1
vote
0 answers

Check if a stubbed getter function has been called with sinon spy

I am using firebase admin and I am trying to write some unit tests for my code. Since admin is injected in my function I figured I could mock a very simple object like this: admin = { get auth () { return { updateUser: () => { …
mikey
  • 1,339
  • 5
  • 22
  • 43
1
vote
1 answer

How to queue something in a Cypress test

I have the following test for a custom Cypress command of mine: it('can navigate to a url', () => { const history = createHistory(); cy.window().then(win => ((win as any).__chh__history__ = history)); cy.spy(history, 'push'); …
Svish
  • 152,914
  • 173
  • 462
  • 620
1
vote
1 answer

When use Spy and PowerMock together throws RuntimeException

When I use Spy annotation together with the PowerMock in my JUnit test case, it throws RuntimeException, but when I use Mock annotation together with the PowerMock, the test case is working fine. Anybody able to advise about my issue? This is my…
by lim
  • 97
  • 1
  • 12
1
vote
1 answer

Is there any way to use callsFake with sinon spies?

I am trying to migrate from expect to chai and Sinon. I expect we do something like this this.check = expect.spyOn(module, "method").andCall(function(dep) { return dep; }); But I want this behavior with Chai and Sinon. this.check =…
Aniketh Saha
  • 843
  • 10
  • 22
1
vote
1 answer

How to spy on a function that is also referenced by a property

Given this code: function getAnimal(type, color) { console.log('blub'); // this is triggered when executing the test } this.getAnimal = getAnimal; and this testing code: describe('getAnimal()', function() { it('should get an animal based on…
GarfieldKlon
  • 11,170
  • 7
  • 31
  • 33
1
vote
0 answers

How do I spy on a method from a sealed library class?

I have an analytics client that has a track method getting invoked when an event is received. When I go to mock the class I get this: Type to mock must be an interface or an abstract or non-sealed class. Is there any way to spy on and verify…
heug
  • 982
  • 2
  • 11
  • 23
1
vote
1 answer

treeModel undefined when test angular-tree-component

I am working with angular-tree-component to show a category tree in a stateless component in angular. Currently, I have up and running the component and is time to make some unit tests. I am experiencing a problem because I want to test a function…
xzegga
  • 3,051
  • 3
  • 25
  • 45
1
vote
1 answer

Using Mockito doReturn always returns null

I am using Mockito for my test class: @RunWith(SpringRunner.class) @Import({MyTestContextConfiguration.class}) public class MyWorkerTest extends BaseWorkerTest { @Spy protected static MyWorkerImpl externalTaskTopicServiceImpl; @Before …
du-it
  • 2,561
  • 8
  • 42
  • 80
1
vote
1 answer

Mocking new instance creation inside testing class using mockito

I am attempting to mock the new instance of an object inside the class I'm testing but I'm struggling to find a way to do this using Mockito. If I understand this correctly, this might be something that can be achieved using Powermock but I only…
Sgr
  • 315
  • 1
  • 5
  • 20
1
vote
1 answer

How to call real method on condition of spied object in Mockito?

I need to be able to call a real method of spy object based on some condition. I.e. if condition is true then call real method otherwise do something else. To be clear, I need to throw an exception on first call and call real method on a second…
Oleksandr
  • 3,574
  • 8
  • 41
  • 78
1
vote
1 answer

How to make sinon mock return different objects on different calls?

mock = sinon.mock(); mock.exactly(2); mock.callsArgWith(1, m1); mock.callsArgWith(1, m2); Here in my test, m2 is overriding m1. I want to return m1 in my first call and m2 in my second call. How to do that?
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
1
vote
1 answer

Sinon Spy never called but it should be

Problem I'm testing a custom redux middleware using Jest and SinonJS and more precisely I want to test if some functions are called on special conditions inside the middleware. I use SinonJS for creating the spies and I run my tests with Jest. I…
Tenshock
  • 107
  • 10
1
vote
1 answer

Jasmine: Testing static function in another class

Assuming i have a static class and a normal class as below. class StaticClass { static staticFunction() { console.log('Static function called.'); } } class NormalClass { normalFunction() { StaticCLass.staticFunction(); } } How do…
Tan Vee Han
  • 65
  • 2
  • 9
1
vote
2 answers

How to capture all mockito spied class method invocations without counting inner delegations

Assuming that with a class like: public class A { public void smth { } public void smth2 { smth(); } } We spy it with mockito and make a simple call : A spiedA = spy(new A()); spiedA.smth2(); After that when we want to…
user2207495
  • 325
  • 2
  • 12