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
4
votes
2 answers
Test millisecods argument on setTimeout with jest
I have a method to trigger a pause using a promise with a setTimeout:
pause = ({ time } = {}) => {
const pauseTime = time || 500;
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, pauseTime)
})
}
I want to test…

Rodrigo
- 71
- 6
4
votes
2 answers
nodejs unit test error "sinon.restore is not a function"
Hey there I am trying to write unit tests by using sinon ,however, I am not able to reset sinon changing behaviour of my stub. First I was having
TypeError: Attempted to wrap getLastData which is already wrapped
After some research I got that I need…

quartaela
- 2,579
- 16
- 63
- 99
4
votes
1 answer
Jasmine spy on exported function on NodeJS
I've had trouble spying on exported function in a NodeJS (v9.6.1) app using Jasmine.
The app is written in typescript, transpiled with tsc in a dist folder to be executed as javascript.
App
I have a Foo utils file (foo.utils.ts) which exports…

Asoub
- 2,273
- 1
- 20
- 33
4
votes
1 answer
Testing an API call in Angular 2
I have the following 2 components and a single service that is shared by both. I need to unit test them but I'm unable to figure out how to test the dependency of the service on the following components.
//a.component.ts
import { Component, Input }…

Sadar Ali
- 153
- 1
- 6
4
votes
1 answer
Jasmine: Real function being called after spyOn with function reference
I have an angular service. Inside this service I have an object with a function, that references another function on the service. (Code below)
I want to use Jasmine (1.3) to spy on my service function, to verify that when the object's function gets…

Mistrog
- 43
- 1
- 6
4
votes
1 answer
Mocha Sinon spy on imported function
How can I spy on an imported function without wrapping all functionality of the import into an object?
Following snippet on how I would like to test:
import {create} from '../../server/session/sessionFactory';
...
create = sinon.spy(create); //…

paesu
- 41
- 1
- 2
4
votes
1 answer
How to spy on a sub-function of a named export in ES6
I want to spy on a sub-function that is exported as a named export, but, it seems like we cannot spy on it.
Let's say I have two functions called add and multiply in operations.js and export them as named exports:
const add = (a, b) => {
return a…

Supasate
- 1,564
- 1
- 16
- 22
4
votes
1 answer
Spy with colors Matlab
With reference to this previous post
Getting different colors for different numbers using `spy` in Matlab
where it was suggested the following in order to have different values of spy represented with different colors
spy(a,'k')
hold…

Matilde
- 353
- 5
- 14
4
votes
1 answer
When running jasmine tests, how can I know if I am in a describe block, beforeEach block or it block?
I need to throw an exception if a utility is used outside of an 'it' or 'beforeEach' block in my tests.
Example -
describe('some test', function(){
useUtil(); // should throw exception
beforeEach(function(){
useUtil() …

Gyro
- 944
- 1
- 8
- 16
4
votes
1 answer
Jasmine-node - Creating a spy on a constructor called inside other function
I'm novice in jasmine and I need to write some unit tests for node.js app in this framework.
I have some problems, one of them is this described below:
var sampleFunction = function(){
var loader = new Loader(params);
// rest of logic…

Grzegorz Pasnik
- 164
- 2
- 7
4
votes
0 answers
How to freeze ahk window spy in windows 8
My Window Spy says "Shift-Alt-Tab to freeze display" but ShfitAltTab winminimizes all windows.
How can I freeze the Window Spy of autohotkey in Windows 8?

AMDG
- 269
- 2
- 9
4
votes
1 answer
How to spy on a property that is not exported
I have a module "sitescollection" like this:
var site = require('./site'); // <- this should be stubbed
var sitesCollection = function(spec) {
var that = {};
that.sites = {};
that.findOrCreateById = function(id) {
if…

roman
- 97
- 5
3
votes
0 answers
Why is Laravel facade spy not working in certain test cases (Laravel testing)
I am studying Laravel's facade spies, but I can't understand why the first test below works and the second doesn't.

Daniel
- 31
- 1
3
votes
1 answer
how spy a defined variable in node.js
Please help me! I have been wrecking my mind but I can not find out how should I stub a variable! Am I wrong? Should I use Spy?
How should I test this code
module.exports = async () => {
var variable = 'something';
var taskProcessor =…

george
- 33
- 3
3
votes
1 answer
Mockito Spy Does Not See Correct Primitive Value
I am facing the below problem where, when indirectly updating the fields on a spy object, the spy does NOT see updates on primitive fields, whereas it sees on reference once.
As an example:
import org.junit.Test;
import org.mockito.Mockito;
import…

nikkatsa
- 1,751
- 4
- 26
- 43