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.

Mocking and faking are ways to isolate code or components to ensure that s 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.

Reasons for use

In a unit test, mock objects can simulate the behavior of complex, real objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test. If an actual object has any of the following characteristics, it may be useful to use a mock object in its place:

  • The object supplies non-deterministic results (e.g., the current time or the current temperature);
  • It has states that are difficult to create or reproduce (e.g., a network error);
  • It is slow (e.g., a complete database, which would have to be initialized before the test);
  • It does not yet exist or may change behavior;
  • It would have to include information and methods exclusively for testing purposes (and not for its actual task).

For example, an alarm clock program which causes a bell to ring at a certain time might get the current time from the outside world. To test this, the test must wait until the alarm time to know whether it has rung the bell correctly. If a mock object is used in place of the real object, it can be programmed to provide the bell-ringing time (whether it is actually that time or not) so that the alarm clock program can be tested in isolation.

Most commonly, isolation frameworks are used to dynamically build a mock, such as:

15654 questions
8
votes
2 answers

Mocking official MongoDb driver

I need to define these interfaces to mock official mongo driver type MgCollection interface { FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult // Other methods } type MgDatabase…
Vahid Jafari
  • 916
  • 6
  • 21
8
votes
1 answer

Jest mock node module not working with typescript

I try to mock the module uuid/v4 coming from npm. To do that, I have created a mock folder as suggested by jest : https://jestjs.io/docs/en/manual-mocks My folder structure : ├──__mocks__ | └──uuid | └──v4.ts ├──src │ └──__tests__ │ …
Putxe
  • 1,054
  • 1
  • 16
  • 23
8
votes
1 answer

How to mock npm module with sinon/mocha

I'm trying to test a function that calls the module cors. I want to test that cors would be called. For that, I'd have to stub/mock it. Here is the function cors.js const cors = require("cors"); const setCors = () => cors({origin:…
Ndx
  • 517
  • 2
  • 12
  • 29
8
votes
3 answers

Mockito, Testing an object that relies on injected dependencies (Spring)?

I'm new to using Mockito and am trying to understand a way to make a unit test of a class that relies on injected dependencies. What I want to do is to create mock objects of the dependencies and make the class that I am testing use those instead of…
Rick
  • 16,612
  • 34
  • 110
  • 163
8
votes
2 answers

How to mock objects methods return value

what I currently have is: def some_method(): some_obj = some_other_method() # This is what I want to mock return value of: some_obj.some_obj_some_method() @patch('some_package.some_other_method') def…
CodeNewbee
  • 176
  • 1
  • 2
  • 9
8
votes
2 answers

How do I mock a user created window property?

I have a javascript utility file that makes API requests. It has two conditional's at the top that rely on window.VarA and window.VarB. Where VarA and VarB are variables given to the window by our software. If they are set the code inside the…
jwolsborn
  • 576
  • 1
  • 6
  • 26
8
votes
1 answer

Move common mocking code to a separate file containing a Jest manual mock

There is a mock I use in many places, so I want to move it into a separate file that can be reused. I think Jest calls this a "manual mock". However I don't want to use the __mocks__ convention. The top of the file being tested: import * as…
lonix
  • 14,255
  • 23
  • 85
  • 176
8
votes
3 answers

Test function with lru_cache decorator

I'm attempting to test a a method that is memoized through lru_cache (since it's an expensive database call). with pytest-mock. A simplified version of the code is: class User: def __init__(self, file): # load a file @lru_cache …
adrpino
  • 960
  • 8
  • 33
8
votes
0 answers

Mocking grpc response message in python

Recently I have been getting my hand dirty to mock gRPC calls in python using MagicMock() but I have been not successful in mocking gRPC response message which I have got. Here is the piece of code python client code def senor_read(self,…
pkumarn
  • 1,383
  • 4
  • 22
  • 29
8
votes
0 answers

Mocking a HttpContext Response.Output with Moq

I've been using the MvcMockHelpers class found at Hanselman's blog for passing in a mocked HttpContext. We extended it somewhat to add some authentication data we needed and for the most part this has been great. The issue we are having is that the…
Sailing Judo
  • 11,083
  • 20
  • 66
  • 97
8
votes
2 answers

WPF/C#: 'Slide to Unlock' feature from iPhone

I just want to ask for your opinion on how to achieve the 'Slide to Unlock' feature from iPhone using Windows Presentation Foundation. I already came across to this article: iPhone slide to unlock progress bar (part 1), and wondering if you can give…
abramlimpin
  • 5,027
  • 11
  • 58
  • 97
8
votes
2 answers

TypeScript doesn't recognize my jest mock module

Suppose I have an index.ts that will import Database.ts and run some queries. To test this index.ts file, I want to mock the Database.ts because I don't want to connect to any real database. This is my index.ts: import { connect } from…
CodinCat
  • 15,530
  • 5
  • 49
  • 60
8
votes
1 answer

Mocking a Microsoft DLL

I have an application that references a Microsoft DLL (Exchange Web Services). In my unit testing, I want to replace the Microsoft DLL with a Mock. What works with other DLLs, doesnt work with this DLL, as i get an exception The located assembly's…
Yiftach
  • 81
  • 2
8
votes
1 answer

Vue.js test-utils How to mock getters from module

In my ContactForm component , I have 2 computed mapGetters computed: { ...mapGetters(["language"]), ...mapGetters("authentication", ["loading"]), the first one is defined in my stoe/getters.js export const language = state => { return…
user762579
8
votes
3 answers

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"); } @PostConstruct public void init() { …
Jan Testowy
  • 649
  • 3
  • 13
  • 32
1 2 3
99
100