Questions tagged [mocking]

Mocking and faking are ways to isolate code or components to ensure that unit tests run against the testable unit of code only without actually utilizing other components or dependencies of an application. Mocking differs from faking in that a mock can be inspected to assert the results of a test.

0
votes
0answers
4 views

Testing a fs library function with Jest/Typescript

I am trying to test a library function that I have written (it works in my code) but cannot get the testing with the mock of the fs to work. I have a series of functions for working with the OS ...
0
votes
1answer
12 views

Mockito verify constructor calls method

I'm trying to test when the Game class is instantiated, that the start method is called. However I get the following error: Wanted but not invoked: game.start(); Actually, there were zero ...
0
votes
0answers
6 views

Autofac in Windows service using Decorator pattern

Is it advisable to use Autofac nuget package or Microsoft Mock framework in financial clients. I am looking to implement Autofac Decorator/ or https://autofac.readthedocs.io/en/latest/faq/select-by-...
0
votes
1answer
27 views

Define a Lambda function as Argument Constraint in Spock

There is a piece of code I want to cover with a unit test public List<Product> fetchProducts() { ... String userId = anotherObj.getId() return caller.call(client -> client....
1
vote
0answers
21 views

Python mock all future usage

I want to mock an object such that all future usage (through imports) in my module would use my mocked object instead of importing the regular object. As far as I understand, mocking something in the ...
0
votes
1answer
17 views

React - Jest Mock an inner component

I have a component that has a child component. This child component has some rules to display its children content, and I want to mock it to display the content regardless its rules. import ...
0
votes
1answer
16 views

Is it possible to mock a method chained through an attribute?

I have the following PHP function, for which I am trying to write a PHPUnit test: public function getDisplayMode() { if($this->request->query->get('master_video')) { return '...
0
votes
1answer
16 views

java.lang.NoSuchMethodError while mocking MessageHandler in Spring Integration

I have a simple Spring Integration configuration that works @Configuration public class FTPIntegration { @Bean public MessageChannel outputIntegrationChannel(ErrorHandler errorHandler) { return ...
1
vote
1answer
18 views

Mock an axios request to external service in test

I'm trying to mock a call to an external service from NodeJS express. I can't get axios-mock-adapter to intercept the actual axios call (http://api.openweathermap.org) and to return a mocked response....
-2
votes
0answers
9 views

Cannot mock the class using mockito in spring weflux

import java.util.Date; import java.util.List; import java.util.function.Function; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http....
-1
votes
1answer
11 views

JMockit Returns Empty Object class

I am new to JMockit. I am trying to mock a class's method but the property are null. Examples below: WebServiceUtility: @Component public class WebserviceUtility { public SamResponse ...
0
votes
1answer
7 views

jest - spy function inside mock function

I have a chained mock function inside another mock function, I need to spy both of them but I don't know how to spy the second one. This is an example: res = { status: jest.fn(() => { ...
0
votes
0answers
22 views

Mock a response for HTTPS server based on certificate

I'm trying to use MockServer to mock an https response based on the certificate. An example would be if a request comes in from a user with certificate A, return a response with body message A and if ...
0
votes
1answer
15 views

Change MagicMock return_value instance method callable to PropertyMock

I'm trying to change the callable for an instance method on my MagicMock to a PropertyMock as that is how it is accessed (Django model column). But test fails with _mock_self = <PropertyMock name=...
-1
votes
2answers
32 views

mock method to do not throw an exception

I have method that throws an exception in special circumstances. I would like to write a test case that will check behaviour when exception is not thrown. I cannot find this in docs or examples. ...
0
votes
2answers
24 views

Override autowired instance with a mock

ServiceInstance.createInstance is supposed to be called when a PUT call is made to the URL below. To be able to test if the correct method is called when a PUT request is sent, I want to mock the ...
0
votes
3answers
37 views

Testing @Postconstruct with Mockito

Why when I injecting mocks via Mockito my @Postconstruckt method is not calling? @Service public class MyService { public MyService() { System.out.println("CONSTRUKTOR"); } @...
-1
votes
0answers
16 views

Java library for http client AND server

I am looking for a library, that have implementation for both client and server. For now i used okhttp as client library, but from now i also need server. I found libraries like javali, spark, etc. ...
0
votes
0answers
15 views

MockResponse setBody not working (OKHttp)

Good day. I'm writing simple test with API calls where Retrofit takes place. The test looks like this mockWebServer.enqueue(new MockResponse() .setResponseCode(200) ....
0
votes
0answers
34 views

How to write a unit test for files removed

I am now fixing a defect, the original code is like below: public void Run() { var tempOutputDir = new DirectoryInfo("d:\Test\"); GenerateFiles(tempOutputDir); var result = ...
-1
votes
1answer
26 views

python unittest mocking / patching

Not wanting to test manually my code, I'm trying to write a test which mocks/patches one of my dependencies (PyInquirer is a pretty neat package which handles the CLI for me - question dicts in, ...
0
votes
0answers
12 views

Mocking location on map for iOS UI Tests?

anyone out there tried mocking location on map for iOS UI Tests programmatically (not using edit scheme > allow location simulation) please?
3
votes
3answers
69 views

Mock dependencies of class under tests

I want to test some code that is written to run on an embedded processor in a Visual Studio Native Unit Test project. The TestMe class has several methods that are good candidates for testing, but ...
0
votes
2answers
21 views

Spy on imported function

I want to spy on a function that is executed as soon as the file is required. In the example below I want to spy on bar. I have the following files. code.ts import {bar} from 'third-party-lib'; ...
-3
votes
1answer
28 views

mocking all classes from a module in python

I have a test_a.py, a.py and b.py in 3 different directories in my test environment. b.py class SBD(): def __init__(self): print("SBD created (In B)") a.py import b from b import * print("In ...
0
votes
1answer
33 views

How to mock import inside a method in python

This is a pretty straightforward question. If I have an import inside a method, how can I mock this specific import? For example: def myFunction(self): auth_token_path = self.authTokenPath() ...
0
votes
2answers
23 views

How to mock multiple objects which will return non-null values

I need help in creating mocking two objects simultaneously. If I set the return value of first mock obj i.e. mockClassA to null, it is working fine. I'm using annotations @Mock, @TestSubject of ...
0
votes
2answers
25 views

My mocked method returns the same instance on each call, how do I get it to return a new instance?

private mockedObject cpMock; @Test public void test() { Manager managerTest = new Manager(cpMock, size); Instance instance = new Instance(size); when(cpMock.requestInstance())....
0
votes
2answers
35 views

In python's unittest, how do I mock a fake folder with fake images inside?

I am trying to create a unit test for a function that reads every image from a folder and saves them in a list. Here is a simplified version of the function: def read_images(directory): ...
1
vote
1answer
34 views

How to check any of multiple overloads called NSubstitute

Scenario I would like to check if a component (the sut) logs error in a particular condition. The ILogger interface constructor injected into the component, and the Error method has 4 overloads. So ...
0
votes
1answer
19 views

How to mock cx_Oracle cursor in Python

I have two below functions in a class and I need to mock the database connection and cursor results. cx_Oracle.connect successfully patched it works as expected. But the cursor, callfunc, fetch_all ...
0
votes
1answer
28 views

shell mock --define from array: ERROR: Bad option for '--define' ("dist). Use --define 'macro expr'

I am currently writing a script which should make it more easy for me to build some RPMs using mock. The plan is to make it possible to add values for the mock (and therefor rpmbuild) --define ...
0
votes
0answers
22 views

Mock function exception to test infinite loop

I am having some trouble phrasing this question to google/past answers so I will try to expand it here. If there is another post I can be pointed to, that would be helpful. I am using unittest.mock ...
2
votes
3answers
38 views

Is it possible to mock service which is created in class under test?

I would like to be able to mock SomeService, however, because it's a maven plugin, I'm out of control how this class is created. I need to create SomeService manually, because I can't use it in ...
1
vote
1answer
30 views

monkeypatch function in module whose namespace was overwritten

I am trying to monkeypatch a function in an external module I use, but monkeypatch can't seem to access the function because the namespace of the module gets overwritten on import. Concretely, I use ...
0
votes
0answers
9 views

Python mocking attributes

I had a previous post with trouble of mocking a socket recv and I ran into another issue in another test using mock that are similar in their errors and I think it comes down to a general mis-...
3
votes
2answers
36 views

Mocking Git Module that is internally used by another Module

I am trying to Unit-Test my module Importer::Git that uses Git.pm from CPAN and I would like to mock the calls to Git::command,Git::repository and Git::command_oneline etc to not actually alter my ...
0
votes
0answers
9 views

Python mocking: missing 1 required positional argument

I'm writing tests for my Flask project, and are trying to mock my database model. The code looks something like this: import unittest.mock @unittest.mock.patch("server.models.user") def ...
-4
votes
0answers
29 views

Mocking individual struct function

I'm a little confused with this case. I have a struct. Let's say: type User struct{} then I have 3 functions for this struct func (u User) MethodA() {} func (u User) MethodB() {} func (u User) ...
2
votes
1answer
38 views

Not able to mock a method

I have this code i want to test in isolation in some_module.py import tarfile def function_to_test(path_to_tar): tar_file = tarfile.open(path_to_tar) for tar_info in tar_file.getmembers(): ...
0
votes
1answer
51 views

Mocking socket.recv()

I want to mock the recv function of a socket and having trouble. Getting errors that I need to actually connect my socket even though I should not have to. Here is all relevant code: Class A: ...
0
votes
0answers
14 views

Mocking a tcp server using a PCAP file in python

I have python code that will connect to a server and continuously retrieve data for storing/setting. I used TCP dump to capture what normal traffic looks like and I want to mock this test server for ...
0
votes
2answers
22 views

Powermock - trying to mock method actually calls method

I have a simple class with a static method which usually throws a null pointer: public class MyClass { private static String s; public static final int myMethod(){ return s.length(); ...
-2
votes
2answers
68 views
+50

Temporary ftp server for testing

I want to write a test for my code which uses a ftp library and does upload data via ftp. I would like to avoid the need for a real ftp server in my test. What is the most simple way to test my code?...
0
votes
2answers
22 views

Ignore chained calls in assert_has_calls with pytest mocker

When using mocker from pytest-mock, how can I use has_calls to check the calls to a specific method without checking all the calls made to the return value of that method? For example: import ...
0
votes
0answers
22 views

How to monkeypatch with a mock and set a return value

I am attempting to test a function that calls subprocess.run. I would like to: Check that subprocess.run is called with the expected arguments Make the exit code zero With the following code: ...
1
vote
1answer
40 views

Method not getting mocked

I am using Mockito to mock a method but the test is running the real method. //Controller @RestController public class Controller { private Utils utils = new Utils(); public String ...
0
votes
1answer
34 views

c# Moq objects void method that changes objects parameter value

I'm trying to use Mocks in my Unit testing but I struggle with below code. I want to Callback from AppendName method in right way so that this method is correctly tested and mocked object name is ...
0
votes
0answers
19 views

testing flask app with mock/patch causes flask_security error regarding missing translation file

I am trying to incorporate mocking and patching into tests of a Flask app. I'm using Python 2.7, the unittest in the standard library, and I installed the mock package; Right now I'm trying to ...
0
votes
0answers
22 views

Rspec: Ask a method to receive parameters when using double one-liner syntax?

When using rspec I know that it is possible to ask a double to receive a message with certain parameters like so: double = double("double") allow(:double).to receive(:add).with("parameter1", "...