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
Questions tagged [spy]
425 questions
183
votes
9 answers
Mockito - @Spy vs @Mock
I understand a spy calls the real methods on an object, while a mock calls methods on the double object. Also spies are to be avoided unless there is a code smell.
However, how do spies work and when should I actually use them?
How are they…

Abhinav
- 1,911
- 2
- 13
- 11
128
votes
4 answers
Difference between Mock / Stub / Spy in Spock test framework
I don't understand the difference between Mock, Stub, and Spy in Spock testing and the tutorials I have been looking at online don't explain them in detail.

letter Q
- 14,735
- 33
- 79
- 118
99
votes
10 answers
How to spy on a default exported function with Jest?
Suppose I have a simple file exporting a default function:
// UniqueIdGenerator.js
const uniqueIdGenerator = () => Math.random().toString(36).substring(2, 8);
export default uniqueIdGenerator;
Which I would use like this:
import uniqueIdGenerator…

thisismydesign
- 21,553
- 9
- 123
- 126
89
votes
3 answers
What is the difference between jest.fn() and jest.spyOn() methods in jest?
I am writing the Unit test cases for my react project and using jest and enzyme for writing test cases. I have read the jest documentation
https://jestjs.io/docs/en/jest-object.html#jestspyonobject-methodname
which explains about jest.spyOn()…

Pradhumn Sharma
- 1,663
- 2
- 10
- 19
74
votes
7 answers
TypeError during Jest's spyOn: Cannot set property getRequest of #
I'm writing a React application with TypeScript. I do my unit tests using Jest.
I have a function that makes an API call:
import { ROUTE_INT_QUESTIONS } from "../../../config/constants/routes";
import { intQuestionSchema } from…

J. Hesters
- 13,117
- 31
- 133
- 249
71
votes
6 answers
Spying on a constructor using Jasmine
I am using Jasmine to test if certain objects are created and methods are called on them.
I have a jQuery widget that creates flipcounter objects and calls the setValue method on them. The code for flipcounter is here:…

gerky
- 6,267
- 11
- 55
- 82
71
votes
2 answers
How to change return value of jasmine spy?
I'm using Jasmine to create a spy like so:
beforeEach(inject(function ($injector) {
$rootScope = $injector.get('$rootScope');
$state = $injector.get('$state');
$controller = $injector.get('$controller');
socket = new…

Paymahn Moghadasian
- 9,301
- 13
- 56
- 94
66
votes
2 answers
Jasmine - Spying on a method call within a constructor
I want to test whether the following method is called with in my Javascript object constructor. From what I have seen in the Jasmine documentation, I can spy on a constructor method and I can spy on methods after an object has been instantiated, but…

stillmotion
- 4,608
- 4
- 30
- 36
58
votes
7 answers
mockito : how to unmock a method?
I have a JUnit class with different methods to perform different tests.
I use Mockito to create a spy on real instance, and then override some method which is not relevant to the actual test I perform.
Is there a way, just for the sake of cleaning…

user1045740
- 581
- 1
- 4
- 3
51
votes
5 answers
Using @Spy and @Autowired together
I have a Service Class with 3 methods, Service class is also using some @Autowired annotations.
Out of 3 methods, I want to mock two methods but use real method for 3rd one.
Problem is:
If I am using @Autowired with @Spy, all three real method…

Shashi K Kalia
- 673
- 1
- 5
- 8
48
votes
7 answers
stubbing process.exit with jest
I have code that does something like
function myFunc(condition){
if(condition){
process.exit(ERROR_CODE)
}
}
How can I test this in Jest? Overwriting exit in process with jest.fn() and returning it back after the test doesn't work, since…

Nick Ginanto
- 31,090
- 47
- 134
- 244
35
votes
2 answers
How to mock/spy an imported function in Angular unit testing
Let's say i have an angular 6 component with a method test which returns some value:
import { doSomething } from './helper';
@Component({
...
})
export class AppComponent {
test() {
const data = doSomething(1);
return…

user7995820
- 412
- 1
- 4
- 10
33
votes
4 answers
Tracking email with PHP and image
I have seen the service like spypig.com placing a small image in the email and tracking when it is opened and from where. They track city, country, IP address etc. How is this done?
How do we know when the mail is opened? And how is the…

esafwan
- 17,311
- 33
- 107
- 166
33
votes
4 answers
Mockito Spy - stub before calling the constructor
I'm trying to spy on an Object and I want to stub a method that is called by the constructor before the constructor calls it.
My class looks like that:
public class MyClass {
public MyClass() {
setup();
}
public void setup()…

Matt3o12
- 4,192
- 6
- 32
- 47
30
votes
2 answers
Mockito + Spy: How to gather return values
I got a class using a factory for creating some object.
In my unit test I would like to access the return value of the factory.
Since the factory is directly passed to the class and no getter for the created object is provided I need to intercept…

Marc-Christian Schulze
- 3,154
- 3
- 35
- 45