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.

Filter by
Sorted by
Tagged with
0 votes
0 answers
12 views

Mock a database connection

I have a db connector that has the connection in the constructor. When I run unit test locally, things seem to pass, but it seems to fail at the build stage in the pipeline due to coverage, as well as ...
PandasM's user avatar
  • 11
0 votes
0 answers
9 views

Unable to cover Consumer function in code coverage for ReceiverOptions.addAssignListener()

I have been trying to increase code coverage on this particular piece of code for a while but can't seem to figure it out. This private method is invoked from within a public method. The public method ...
tripletk's user avatar
0 votes
0 answers
7 views

Vitest mock doesn't mock modules

I have this simple example where I want to modules in test cases but vi doesn't mock and returns the original function. I'm using vite "vite": "^3.2.0", "vitest": "^...
Nick's user avatar
  • 2,808
0 votes
1 answer
6 views

Vitest failing when calling tRPC createCaller because the content contains invalid JS syntax (add "**/*.html" to `assetsInclude`)

I'm writing unit tests using Vitest and calling the tRPC createCaller in order to mock the trpc context. When running the tests it's failing on the following error: Error: Failed to parse source for ...
Yaman Ktish's user avatar
0 votes
0 answers
23 views

how to mock a function that returns multiple dataframes

I have a function that returns multiple pandas dataframes. I'm trying to mock it and it is returning as a tuple not as a dataframe. How can I get it as a dataframe? Thank you! def _func() -> Tuple[...
Kingston X's user avatar
0 votes
0 answers
4 views

Does anyone know how to setup WireMock.Net to have cached response?

In WireMock.Net, how can we get response cached after first time we're retrieving API? Does anyone know any specific configuration or how to set it up. Thank you in advance. I tried with different ...
Sandy's user avatar
  • 1
0 votes
1 answer
19 views

how to run the Gitlab pipelines periodically to ensure they are stable

I work on multiple projects that use Gitlab pipelines for building, linting, sonar analysis, jira check and deployment, etc. Sometimes, the stage related to sonar check fails whenever that 3rd party ...
vinod827's user avatar
  • 1,176
0 votes
0 answers
15 views

I copied a unit test almost exactly the same but in mine when I asset_call_once_with I get an extra `bind` argument?

The first of the unit tests below passes, but the second one fails. # unit test passes def test_fetch_display_name(self, rows, expected_display_name, index): """SQL is executed and ...
jikf's user avatar
  • 21
1 vote
1 answer
41 views

How to test if ancestor's __init__ was called with correct arguments

I'm writing unit test for class B, which should check that when I create B() - correct call of super().__init__ was executed. I'm using this code: import pytest class A: def __init__(self, some_val,...
archjkeee's user avatar
0 votes
0 answers
14 views

How can I set a method to point to anything in my unit test using MagicMock?

I have the following class, function and unit test, but I am running into an error when I try to mock out a certain method. Any ideas how to achieve what I desire below? class Index: def ...
jikf's user avatar
  • 21
0 votes
0 answers
8 views

NSubstitute generic interface method setup throws NSubstitute.Exceptions.AmbiguousArgumentsException

I have the following interface definition public interface INotificationRecipientsProvider<T, E> where T : EventBase<E> where E : class { IList<string> ...
Kostas Christodoulou's user avatar
0 votes
0 answers
12 views

store.getActions() not returning actions

I am trying to test if actions are being dispatched correctly. I am using store.getActions(). It is working on one test but not in another. store.getActions() is not returning any results in the ...
Richie's user avatar
  • 19
1 vote
1 answer
22 views

How do I avoid wrap test in act(() =>{} warnings when trying to mock multiple API with jest-fetch-mocks at once in jest?

I am trying to write unit tests for a react application that calls multiple static API endpoints to populate a bunch of different dropdown element options. A stripped down version of my react ...
VoodooJellyfish's user avatar
0 votes
1 answer
42 views

Python patch object, how to assert on calls

I'm trying to create some pytest unit tests on a custom ansible module. I'm trying to mock the boto3 client and just test the logic of the class. I moved the initialization of boto into a class method ...
robobandit's user avatar
-1 votes
1 answer
52 views

Why can non-empty mocked list not be looped?

I am writing a unit test for a simple method. Since only a single class should be tested in a unit test, everything else must be mocked. The method accepts a single list; Mocking the input list ...
knittl's user avatar
  • 247k
1 vote
1 answer
18 views

Mockoon filter data bucket by multiple body values

I'm trying to mock a HHTP post request/response with Mockoon. The request sends a list of IDs like so: { "ids": [ "d29688fee0d24ed9889f1d1d17062166", "...
kacho's user avatar
  • 381
0 votes
1 answer
34 views

Rust mocking `std::process::Child` for test?

I have an application that spawns a long running Child process. I want to test the logic of the application and the child struct is not really needed for this, I've looked at several mocking ...
Nino Martinez Wael's user avatar
0 votes
0 answers
10 views

Getting error when trying to mock class method to return value it is call real method

Below is the test class ` @RunWith(Parameterized.class) @SpringBootTest(classes = TelelogServiceApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ActiveProfiles("...
kee's user avatar
  • 11
0 votes
0 answers
20 views

How to mock read and write a single file after updating its contents using mock_open in Python

def simple_read_write(): with open("file", "r") as f: data = json.load(f) data["change"] = "some content" with open("file", &...
Sanjay Nayak's user avatar
-1 votes
2 answers
57 views

how to mock in python API testing [closed]

I'm trying to mock a sample function below. Somehow I'm getting error. For example, I've a class as below myclass.py: import os, requests class MyClass: def __init__(self, login_url): ...
Temp Expt's user avatar
  • 296
0 votes
0 answers
19 views

Test in Spring boot @MockMvcTest the MockHttpServletResponse always return a empty body

I'm using mockito to make unit test in my controller, but when I execute the test I have status 200 with an empty body in MockHttpServletResponse. I would like to test the body response with jsonPath ...
Daniel Soto Fuya's user avatar
0 votes
1 answer
16 views

NoInteractionsWanted exception

I'm writing test for my Service written in pure Java that will later migrate to Spring boot. This class just reads several scenarios from json and does infinite loop handling each scenario every time ...
Yurii's user avatar
  • 77
0 votes
1 answer
42 views

Mocking object construction in unit tests

I would like to write unit tests for a class which creates an instance of a problematic class, and I am working under the following constraints: My class calls some third-party library initialization ...
Danilo Piazzalunga's user avatar
1 vote
2 answers
61 views

Mocking Kotlin higher order functions in Spock

Unable to figure out how to mock higher order functions using spock. Example code snippets: import jakarta.inject.Singleton @Singleton class SomeClass { fun bar(function: () -> Unit) { ...
HoneTheRat's user avatar
0 votes
0 answers
16 views

PHP - Mockery accessing to static property when overload a class

I'm trying to write a Test for a esential part of the App, but I don't want to refactor the whole app. I have "hard" dependency between classes. I know, I know, Don't ask me, It's already ...
nicolasst22's user avatar
-1 votes
0 answers
10 views

Mock the same component with different props

I have the component: const ComponentA = ({ items }) => { return ( <> <Row> <Col md="4"> <ComponentField label="ProductID">...
Ingrid Rauany's user avatar
0 votes
0 answers
14 views

Using ng-mocks, how do I render a template that is not wrapped by any directive or component?

I'm trying to test a simple Angular component scenario. I have a component, and in it there's a template that renders some content. This template can later be used for anything. I would like to write ...
Eyal's user avatar
  • 193
-1 votes
2 answers
43 views

Moq not running correctly and always gives wrong result

I created a small test project in VS 2022 -> .Net framework 4.7.2 to check the usage of moq. Interface and the implementation are provided below. public interface IDataMaintainer { bool AddData(...
user413088's user avatar
0 votes
0 answers
17 views

Calling Automocker.Use() with a null parameter

We have a unit test written using Moq.Automocker 3.5.0 which attempts to do the following: IFoo nullFoo = null; _automocker.Use<IFoo>(nullFoo) However, since this change to Moq.Automocker, ...
stuwilmur's user avatar
  • 112
1 vote
0 answers
35 views

Creating a Mock API Platform Using Spring Boot: How to create an API at runtime?

Currently, I'm trying to develop a Mock API platform in Spring Boot that can create a mock RESTful endpoints for developer testing purposes. Specifically, I need to create a system where user can ...
Mr.Han's user avatar
  • 11
0 votes
1 answer
25 views

mocking a read to a file with a non-standard library

I would like to set up a test and mock a call which reads an attribute from a file. I'm using the non-standard library h5py, but this question could apply to other data reading libraries. For example, ...
Rachel W's user avatar
  • 125
0 votes
0 answers
21 views

Using GPS spoofing, is it possible to tell a specific app that I'm in two locations at the same time?

I have a rooted/magisk Android phone and can change the location with mock locations + fake gps app, but I would somehow like to be in two places at once. Is this possible? Android restricts the use ...
Lef's user avatar
  • 3
-2 votes
0 answers
25 views

Question for the PRO's. Understand Python's Unittest Mock Library code

Context. After applying a patch mocking a method inside a class I was trying to understand the content of the resulting mock object and started to explore Python's Unittest Mock Library code. My ...
jojosilva's user avatar
0 votes
1 answer
25 views

Writing unit tests in the presence of a module init function in Ballerina

I have a package like the following: FooClient ep; function init() returns error? { do { ep = check new ("https://some-api", "id", "secret"); // do ...
Fathima's user avatar
  • 85
0 votes
0 answers
18 views

Microsoft Orleans, Mock and IGrainObserver

I am trying to run this test but I get an error. [Fact] public async Task Subscribe() { var cluster = new ClusterFixture().Cluster; var grain = cluster.GrainFactory.GetGrain<INewsGrain>(...
user45245's user avatar
  • 845
0 votes
0 answers
28 views

General Java unit test question. I have to mock a method (with a List parameter) inside the method I'm testing, Issue is with syntax

Way too much code to post here so let me try to be more specific. I'm charged with writing unit tests on a lot of older code. I ran into a method that needs testing, but it calls a second method in ...
CaptainPaul's user avatar
0 votes
0 answers
31 views

SQLalchemy: how to patch session from context manager and replace with in memory DB session

Say I have an SQLalchemy model like: from app.data_structures.base import ( Base class User(Base): __tablename__ = "users" user_name: Mapped[str] = mapped_column(primary_key=...
abinitio's user avatar
  • 607
0 votes
2 answers
36 views

Mock a class method before making an object of that class in setUp

How can I use mock to avoid executing the some_thing method of Settings class and make it return some arbitrary string? My class: class Settings(): """ Represents settings. Will ...
Zloj's user avatar
  • 2,245
0 votes
0 answers
17 views

Cannot setup a mock on a Data Provider

So, I am trying to test a service by setting up a mock of one the data providers it uses. I set up my service and initialize it with an instance on the Mock.Object of my data provider. In my test case ...
KChimev's user avatar
  • 11
0 votes
2 answers
96 views

Assertions not satisfied while mocking a route's endpoins, which end up calling the real ones

I have defined the following route: from(direct(Routes.SEND_EMAIL_BOP_SINGLE_PER_USER)) .routeId(Routes.SEND_EMAIL_BOP_SINGLE_PER_USER) // Split the usernames on an individual exchange each. ...
MikelAlejoBR's user avatar
0 votes
0 answers
14 views

Intermittent Test Failure when Checking for Unique Field Constraint in Mongoose

I am having an issue with one of my Jest tests that verifies the uniqueness of an access_code field in a Mongoose model. The test intermittently fails and I can't seem to pinpoint why. Here's the test ...
Camille's user avatar
  • 13
0 votes
0 answers
24 views

How to mock a nested function of a smart contract?

In a foundry test I can mock the result getReserves() by using: vm.mockCall( poolAddress, abi.encodeWithSelector(IUniswapV2Pair.getReserves.selector), abi.encode(...
AnastasiaShishkova's user avatar
-1 votes
1 answer
28 views

How to mock a dispatch made in a useState?

I have a component that displays a skeleton when a status is "idle", and dispatch a request with react-redux to get data. I am writing the test file for this component, and I want to render ...
Johan's user avatar
  • 2,127
0 votes
0 answers
16 views

How to Resolve "Cannot access 'mockKeycloak' before initialization" Error in Jest and VITest?

I'm writing tests using Jest and VITest for a React application. I've encountered an error related to the initialization order of mock functions and variables. The error message is "Cannot access ...
CoolLife's user avatar
  • 1,419
0 votes
0 answers
52 views

jest throw TypeError: Cannot read properties of undefined (reading 'then') when trying to mock fetch request

I'm trying to mock fetch request using Jest. Here's my App.tsx import { useEffect, useState } from 'react'; import './App.css'; function App() { const [joke,setJoke] = useState("") ...
Ristirianto Adi's user avatar
0 votes
0 answers
33 views

how to write test to check fallback in django function?

i have a function read data from redis and if redis is down it read data from database. def read_data(): try: # read from redis redis_handle = redis.Redis(host=redis_host, port=...
HesamHashemi's user avatar
0 votes
2 answers
45 views

Does Laravel have support for mocking database calls so we don't have to test our database?

In many frameworks like Springboot, mocking the database is common practice because it's an external resource and it can increase latency with testing. I haven't seen that so far from Laravel. Is ...
Drew Gallagher's user avatar
1 vote
1 answer
72 views

SyntaxError: Unexpected string in msw

Recently i started getting this error for mock service worker (msw) in my react project. Details: Node v16.19.1 vite/3.2.6 linux-x64 node-v16.19.1 react 18.2.0 Checked google, github issues but no ...
targhs's user avatar
  • 1,497
0 votes
1 answer
24 views

Mock Controller constructor parameters with fakeeasy in dotnet

I am try to write a unit test for the controller in .net. Actually I am new to .net framework. When I try to mock the controller with Fakeeasy I reserved a error message. Some one can help me to solve ...
Kavishka Madhushan's user avatar
-1 votes
0 answers
31 views

Trying to mock a request with FormData object in the body

i have this request: const formData = new FormData(); formData.append("File", File); fetch(`${MY_URL}`, { method: "POST", body: formData }) and i try to mock ...
user18426672's user avatar

1
2 3 4 5
314