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
27
votes
1 answer
How to spyOn a static class method with Jasmine
I have a class with a static method that I want to test in Jasmine. I understand that static methods are not callable on instances of the class. So besides the fact that it cannot find the method to spyOn, my test does not pass, but how would one go…

Sherman Hui
- 938
- 3
- 10
- 22
26
votes
8 answers
jest typescript - Mock Date constructor
I'm trying to mock new Date() to return a specific date. The following code:
const now = new Date()
jest.spyOn(global, 'Date').mockImplementation(() => now)
gives a compilation error: Argument of type '() => Date' is not assignable to parameter of…

Ella Sharakanski
- 2,683
- 3
- 27
- 47
23
votes
2 answers
Reset call on Jasmine spy does not return
I'm using a Jasmine (2.2.0) spy to see if a certain callback is called.
Test code:
it('tests', function(done) {
var spy = jasmine.createSpy('mySpy');
objectUnderTest.someFunction(spy).then(function() {
expect(spy).toHaveBeenCalled();
…

Jorn
- 20,612
- 18
- 79
- 126
20
votes
5 answers
SpyOn a backbone view method using jasmine
I have a backbone view and I want to create a test to confirm that a click event on some element will call the function bound to that element.
My view is:
PromptView = Backbone.View.extend({
id:"promptPage",
attributes:{
…

mishod
- 1,040
- 1
- 10
- 21
19
votes
2 answers
Mockito cannot create Spy of @Autowired Spring-Data Repository
I am trying to overlay my whole test environment with Mockito.spy functionality so whenever I want i can stub a method but all other calls go to default functionality.
This worked very well with the Service layer but I have problems with the…

a-kraschitzer
- 191
- 1
- 1
- 4
19
votes
2 answers
How to use Jasmine spies on an object created inside another method?
Given the following code snippet, how would you create a Jasmine spyOn test to confirm that doSomething gets called when you run MyFunction?
function MyFunction() {
var foo = new MyCoolObject();
foo.doSomething();
};
Here's what my test…

thinkbigthinksmall
- 894
- 5
- 10
- 19
17
votes
5 answers
How to spy on window.scrollTo in Jest?
I got a very simple react component with following functionallity:
componentDidMount() {
window.scrollTo(0, 0)
}
It seems that you cannot do something like
window.scrollTo = jest.fn()
to spy on scrollTo function.
Therefor, I want to know what…

Amir Hoseinian
- 2,312
- 2
- 24
- 27
16
votes
1 answer
How to spy on a class property arrow function using Jest
How can I spy on a class property arrow function using Jest? I have the following example test case which fails with the error Expected mock function to have been called.:
import React, {Component} from "react";
import {shallow} from…

DanielGibbs
- 9,910
- 11
- 76
- 121
16
votes
2 answers
Robolectric buildActivity() with Mockito spy?
It seems to me that building an Activity unit test with Robolectric's lifecycle utilities (starting with Robolectric.buildActivity()) and spying on the same Activity with a Mockito spy are mutually exclusive.
Because buildActivity() controls the…

espressoexcess
- 191
- 1
- 3
14
votes
4 answers
Error: : function is not declared configurable
I had working jasmine tests with webpack 3. Now I try to use it with webpack 4 but have some problem with it.
Firstly I had problem with spyOn function.
Error: : myFunction is not declared writable or has no setter
I found some articles about…

konrado
- 185
- 1
- 1
- 8
14
votes
2 answers
Jest mocked spy function, not being called in test
The component:
The Input component with the onChange handler:

Leon Gaban
- 36,509
- 115
- 332
- 529
14
votes
1 answer
How to test that a function has been called after an event was fired?
There is custom event fired in the FooView ..
// views/foo_view.js
this.trigger("something:happened");
The associated FooController binds a handler to take care of the event ...
// controller/foo_controller.js
initialize: function() {
…

JJD
- 50,076
- 60
- 203
- 339
13
votes
1 answer
How to spyOn method inside prop passed down to a component using Jest?
Background:
My test Framework is Jest and Enzyme. I have a Component called Lazyload that is coupled to a LazyloadProvider using React.ContextAPI. I would like to write a test that guarantees that on componentDidMount of the Lazyload Component…

Armeen Moon
- 18,061
- 35
- 120
- 233
13
votes
1 answer
expected a spy but got undefined?
Trying to get my head around jasmine spies, this is what my test looks like:
$scope.switchTurns = function () {
$scope.playerTurn = !$scope.playerTurn;
console.log($scope.centrePileCards.length);
if ($scope.playerTurn == 1) {
…

Pindakaas
- 4,389
- 16
- 48
- 83
12
votes
1 answer
Is there a spyOn analogue in QUnit?
I'm writing specs for different test cases for Jasmine and QUnit to compare them and they looked the same before I needed to write a test to check if an event is binded to an element.
Event binding looks like
$('.page').live('click', function() {…

Daniel J F
- 1,054
- 2
- 15
- 29