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
TypeError: Cannot read property 'then' of undefined during Testing React with Axios
Header.js
const formSubmitHandle = () => {
if (string !== "") {
httpService.fetchData(var1, var2).then((res) => {
//stuff to do...
});
} else {
console.log("Type something to search");
…

Lajos Bela
- 49
- 6
1
vote
0 answers
Mockito.spy() instance returns null when invoking real method
I want to test a service class.
class Service {
public List mainMethodToTest() {
//calling another method to get some values
List list = getImportantValues();
process list;
return list;
}
public getimportantValues() {
return…

Manni
- 27
- 1
- 7
1
vote
0 answers
Value passed to argumentMatchers.eq() appears null
I'm mocking a method getQuizFromQuizResponse that requires a non-null object. I tried using eq() from the ArgumentMatchers library to pass in an existing object I initialized. When I debug SecurityServiceTest in my IDE, eq(quizResponse) appears…

frlzjosh
- 410
- 5
- 17
1
vote
0 answers
Is it possible to spy on an object in which I have no control over it's creation, using mockk?
I have a Kotlin project using kotest as my unit testing framework and mockk to take care of my mocking requirements. I am new to all aspects of the Kotlin language and chosen testing libraries/frameworks.
I have a simple class in which a function…

user16039166
- 35
- 6
1
vote
1 answer
Mockito/Spy not working, executing the original function
I am having a class :
GP_CategoryService.java
Function -->
public JSONObject deleteCategory(GP_CategorySubcategoryBean bean) {
JSONObject data = new JSONObject();
DirectoryCategoryMaster oCategory = getCategoryMaster(bean);
…
user14748001
1
vote
1 answer
jest spyOn navigator.mediaDevices
I am writing a small library at the moment to help with connecting to media devices, it would be nice if I could unit test the library, I have a function in my typescript library that looks like this,
static connectAudioDevice(device?:…

Udders
- 6,914
- 24
- 102
- 194
1
vote
2 answers
How to reset a spy or mock in jest
I have a function that I have mocked in my test cases file.
MyService.getConfigsForEntity = jest.fn().mockReturnValue(
new Promise(resolve => {
let response = [];
resolve(response);
})
);
Now I want all my test cases in that…

StrugglingCoder
- 4,781
- 16
- 69
- 103
1
vote
1 answer
spying an array element with mockito returns wanted but not invoked when the method is invoked
**UPDATE : I have misunderstood the way spys function completely, I should be calling the spy version of a method in order for it to be verified NOT the real object method Ex : r.getPrice(); then verify(r).getPrice(); I still haven't figured the…

A.Mahmoud
- 65
- 7
1
vote
0 answers
Spy chained functions in Python
I am trying to unit test a function that is calling two other functions internally. It looks like the following
class A(BaseClass):
def functionA(self, var_a, var_b):
return self.another_fn_a(var_a).another_fn_b(var_b)
another_fn_a and…

aakashgupta.0205
- 647
- 1
- 8
- 23
1
vote
2 answers
Angular SpyOn public property
I need to spy on the public property in angular service. But this property doesn't have getter or setter, therefore, I couldn't spy on that property.
Always get or set is required to use that SpyOn. If I don't provide it, it is set as 'get'. Is…

PushpikaWan
- 2,437
- 3
- 14
- 23
1
vote
1 answer
Jasmine: mock method response based on method's parameters
My question is simple. I want to mock a method ONLY if a certain value is passed to the method. Something like this:
spyOn(myObj, "getUser").and.withArgs('userA').and.returnValue(null);
If the method getUser is invoked with the value 'userA', the…

Perimosh
- 2,304
- 3
- 20
- 38
1
vote
2 answers
Difference between a spy and a mock with delegation in Mockito
I'm writing integration tests for an application that gets certain objects from Kafka, validates them, performs various conversions and either saves converted objects to another database or sends a response with errors back to Kafka.
The production…

John Allison
- 966
- 1
- 10
- 30
1
vote
1 answer
Form validation using Spy by Sinon gets wrong
I'm unable to verify if a form is rightfully filled using the Spy() function of Sinon.
My goal is to pass this unit test:
it('check entire form validation when the form is valid', () => {
let formSpy = spy();
const form = mount(
1
vote
0 answers
How can I make Input setter Function is called while testing
Im trying to spy a function of a component. I can spy that but my test code does not invoke the Input setter of this component.
export class CameraComponent implements OnInit {
liveData: LiveDataModal[];
@Input('liveData')
set…

feyzullahyildiz
- 456
- 4
- 11
1
vote
1 answer
Using jest spyOn cannot detect methods being called inside try-catch block
The problem I'm having is that Jest is reporting setResultsSpy is being called 0 times when in fact, I know that it is being called. I know this by putting console.log(results) under the const results = await getFileList(data.path); in my code and…

Alex
- 1,293
- 1
- 13
- 26