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
6
votes
1 answer

How to validate the return value when calling a mocked object's method

Using Mockito, is there a way to spy() on an object and verify that an object is called a given # of times with the specified arugments AND that it returns an expected value for these calls? I'd like to do something like the following: class…
Chris Morris
  • 4,335
  • 4
  • 24
  • 28
6
votes
6 answers

mockito better expected exception test using spy

How can I make the 3rd test to check for the existence of cause1 in the message of the exception? I also listed in the first two tests that have drawbacks. First is not checking for the message second needs a lot of boilerplate code. public class…
raisercostin
  • 8,777
  • 5
  • 67
  • 76
5
votes
2 answers

Jest: Standard way to stub named exports of ESM modules

This question is not specific to Jest as it applies to all testing libraries with stubbing capabilities. ESM modules have immutable named and default exports, which means this is no longer valid: // @filename foo.mjs export function foo() { ...…
ldiqual
  • 15,015
  • 6
  • 52
  • 90
5
votes
3 answers

How to unit test clipboard copy in angular?

How to spy on clipboard.copy method? For const clipboard = TestBed.inject(Clipboard); spyOn(clipboard, 'copy').and.returnValue(true); I get warning that Argument of type '"copy"' is not assignable to parameter of type 'keyof Clipboard'. I've also…
Sakén
  • 197
  • 1
  • 12
5
votes
0 answers

Spy on jasmine function from an Angular library is not working

I have an Angular library containing functions like "export function myFunction". The project with the error have this library as a dependency and when I want to spy on the function, the error below is display: myFunction is not declared writable or…
C0ZEN
  • 894
  • 11
  • 41
5
votes
1 answer

How to mock Abstract class method in a class to be tested

There is an abstract class public abstract class BaseProcessor { public BooksTransaction getBooksTransaction() { return booksTransaction; } } There is another final class which is to be tested using Junit public final class…
5
votes
2 answers

Use `jest.spyOn` on an exported function from a Node module

In Jest, to spy (and optionally mock the implementation) on a method, we do the following: const childProcess = require('child_process'); const spySpawnSync = jest.spyOn(childProcess, 'spawnSync').mockImplementation(); This allows us to use…
Gary
  • 3,891
  • 8
  • 38
  • 60
5
votes
1 answer

Jest onSpy - expected mock function to have been called

I'm struggling with using spyOn as part of testing my utils.js module. I've tried various methods and approaches but all seem to yield the "expected mock function to have been called". For the record, other unit tests work OK, so there shouldn't be…
terjeofnorway
  • 53
  • 1
  • 1
  • 4
5
votes
1 answer

Injection an object though InjectMocks Spy

I need to run series of unit tests over a class, which has a @Autowired Logger implementation. The base idea of realization was: @Mock Logger logger; @InjectMocks TestedClass tested; but i want to save the logging output functionality. Does…
Draaksward
  • 759
  • 7
  • 32
5
votes
1 answer

Cannot spy on angular.element

I have one Jasmine test that is continously failing due to a spyOn not executing. The following test will automatically fail: it('simple test', function() { spyOn(angular, 'element'); }); The error is: TypeError: 'undefined' is not an object…
geoff
  • 2,251
  • 1
  • 19
  • 34
5
votes
2 answers

In JUnit with Spring, how do I create a spy on a @Service using Mockito (1.10.18)?

I’m using Spring 3.2.11.RELEASe, JUnit 4.12, and Mockito 1.10.18. In my JUnit test, how do I create a spy (not a mock, a spy) of an @Autowired spring service? Here’s how the service is declared … @Service("orderService") public class…
Dave
  • 15,639
  • 133
  • 442
  • 830
5
votes
1 answer

Spying on immutable native methods

I want to test if window.location.assign is being called, and so am trying to use spyOn(window.location, 'assign');, but the method isn't overwriteable. Are there any other approaches I can use to spy on native methods that can't be overwritten?
wheresrhys
  • 22,558
  • 19
  • 94
  • 162
5
votes
2 answers

Howto get a callback on JS function execution using a Spy

I want to spy on a function, then execute a callback upon function completion/initial call. The following is a bit simplistic, but shows what I need to accomplish: //send a spy to report on the soviet.GoldenEye method function var james_bond =…
3.3volts
  • 178
  • 1
  • 6
4
votes
2 answers

Skipping a method execution using Mockito

I’m using Mockito for unit testing and I want to skip the execution of a method. I referred to this ticket Skip execution of a line using Mockito. Here, I assume doSomeTask() and createLink() methods are in different classes. But in my case, both…
4
votes
1 answer

jest.spyOn is not working when called from a side effect call

I have the following scenario: const doSomething = () => { const test = 1; doAnotherThing(); return test + 1; }; const doAnotherThing = () => {}; module.exports = { doSomething, doAnotherThing }; And here is my test: const…
Iuri
  • 166
  • 1
  • 9