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
1
vote
0 answers
Need help to write jasmine unit test case for print functionality
I have searched solution for this scenario. I found nothing. So, I am posting as a new question. I have a method in component which prints data in document like (control+P).
printContents;
popupWin;
print(): void {
this.printContents =…

Narayana Bojja
- 51
- 7
1
vote
1 answer
Junit Mocking/Stubbing method B inside method A (Non Parameterized method A)
I came across a scenario where I am not able to mock/stub a method.
Class A{
B b = new B();
method aa(){
... call to method bb in class B
}
}
Class B{
method bb(){
......
}
}
I want to mock method bb for class B.
Since method…

Rishabh Agarwal
- 2,374
- 1
- 21
- 27
1
vote
2 answers
Spy on Angular5 component not working properly
My component has the following function:
updateTransactions() {
let notes = this.createNotes()
let delTransactions = this.createDelTransactions()
this.noteService.createNote(notes[0])
…

Shamoon
- 41,293
- 91
- 306
- 570
1
vote
1 answer
modify private data members using spyon.and.callFake in Jasmine
I have a public method A() in service which modifies a private data member of that service.
A() {
//call http
// change private data member using call's result
}
I am unit testing a component that uses this service and thus want to change the…

AvinashK
- 3,309
- 8
- 43
- 94
1
vote
1 answer
How to spy with sinon on callback calls triggered by EventEmitter events? Javascript, ES6, Unit-tests, Chai
I need to test whether my callback was called n number of times and always returned true.
Here is my test callback function in typescript:
const checkBlockTransaction = (block: ILogsBlock) => {
const tx = transactions.find(element =>…

jeff
- 1,169
- 1
- 19
- 44
1
vote
1 answer
Sinon spy on switch statement
Is there a way to spy / stub a switch statement? I tried:
let spy = sandbox.spy(global, 'switch');
But that does not work unfortunately.

Mankind1023
- 7,198
- 16
- 56
- 86
1
vote
1 answer
sinon - spy on toString method
In my file, I have something like this:
if(somevar.toString().length == 2) ....
How can I spy on toString from my test file? I know how to spy on things like parseInt with:
let spy = sinon.spy(global, 'parseInt')
But that doesn't work with…

Mankind1023
- 7,198
- 16
- 56
- 86
1
vote
0 answers
SpyOn jquery selector function call jasmine
I would like to spy on the is(':focus') function of a jQuery selector.
It looks as follows:
$('.some-input-class').is(':focus')
Some of my attempts are:
spyOn($('
'), 'is').andReturnValue(false)…

Kratos
- 1,064
- 4
- 20
- 39
1
vote
1 answer
How to spy on constructor using Jasmine and TypeScript
Is there a way how to spy on constructor using Jasmine and Typescript?
Proposed solutions in question Spying on a constructor using Jasmine do not work because TypeScript does not expose global declarations on window.
Here is my use case:
//…

Radek Matěj
- 569
- 6
- 17
1
vote
1 answer
Spock bug? callRealMethod() on stub does not throw expected exception
Extract from class CUT under test:
def compileOutputLines( TopDocs topDocs ) {
println "gubbins"
}
Test code:
def "my feature"(){
given:
CUT stubCut = Stub( CUT ){
compileOutputLines(_) >> { TopDocs mockTD ->
// NB…

mike rodent
- 14,126
- 11
- 103
- 157
1
vote
1 answer
Spock spy - spy class that used in function (Java)
How can I spy for the Class File in anthoer function in class?
Works for the test when I use new File in the test but not in my class.
public class Clazz {
public void fun(String path) {
File file = new File(path);
if…

Kfir
- 11
- 2
1
vote
4 answers
Java mock function to change its behavior
I am working on writing a unit test for piece of class like this:
public class AClass{
private List testStringList;
public void upperMethod(){
testStringList = new ArrayList();
// add some thing to the…

Robinson Wei
- 143
- 3
- 13
1
vote
2 answers
Redux-Form : How to mock formValueSelector in Jest
I have a component Demo whose Label depends on the current value of a field in the redux-form state. I am using formValueSelector to get the current value of "param" field from the form state. It works fine. However, while running npm test, the…

Charanjeet Kaur
- 1,199
- 3
- 14
- 27
1
vote
1 answer
How to set markersize in plt.imshow()
I have the following problem:
I want to plot an adjacency matrix using a colormap. Now I want do adjust the markersize, because you cannot really
see the dots in the picture since the matrix is really big . How can I do this? Using spy(), this works…

Henrik
- 521
- 3
- 7
- 16
1
vote
1 answer
how to spy on a function
I am trying to spy on a global function e.g.
function foo() {
}
but the below test is failing, how to do that
var spy = sinon.spy(foo);
foo();
expect(spy.callCount).to.equal(1);
** EDIT **
If I do it like below then it works
var…

harishr
- 17,807
- 9
- 78
- 125