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
3 answers

Testing Node.js, mock out and test a module that has been required?

I am struggling to write high quality tests around my node modules. The problem is the require module system. I want to be able to check that a certain required module has a method or its state has changed. There seem to be 2 relatively small…
henry.oswald
  • 5,304
  • 13
  • 51
  • 73
8
votes
1 answer

Unit Testing REST API

I have some experience with unit testing and mocks. In my limited experience I would use the two to test a service layer, for example, mocking (stubbing?) out the database to eliminate dependencies and concentrate on unit testing the business…
Mike
  • 2,912
  • 4
  • 31
  • 52
8
votes
1 answer

C function call and parameter tracing - test case and mock generation

I have a large code base of quite old C code on an embedded system and unfortunately there are no automated test cases/suites. This makes restructuring and refactoring code a dangerous task. Manually writing test cases is very time consuming, so I…
trenki
  • 7,133
  • 7
  • 49
  • 61
8
votes
6 answers

How to mock object construction?

Is there a way to mock object construction using JMock in Java? For example, if I have a method as such: public Object createObject(String objectType) { if(objectType.equals("Integer") { return new Integer(); } else if…
Grundlefleck
  • 124,925
  • 25
  • 94
  • 111
8
votes
3 answers

Should static classes be avoided because it makes Dependency Injection Difficult?

Somebody tasked with creating a "Core" set of libraries created a set of static classes providing all sorts of utilities from logging, auditing and common database access methods. I personally think this stinks because we now have a Core set of…
Ryu
  • 8,641
  • 10
  • 65
  • 98
8
votes
2 answers

What is the best way to mock a 3rd party object in ruby?

I'm writing a test app using the twitter gem and I'd like to write an integration test but I can't figure out how to mock the objects in the Twitter namespace. Here's the function that I want to test: def build_twitter(omniauth) Twitter.configure…
spinlock
  • 3,737
  • 4
  • 35
  • 46
8
votes
3 answers

Mock call to write()

I have a logger class that needs to write strings to file. So, I have a method like so: def write_to_file(self, string): self.__file_handle.write(string) Note that error handling has been edited out. Clearly I want to test this without writing…
Sardathrion - against SE abuse
  • 17,269
  • 27
  • 101
  • 156
8
votes
3 answers

Unit testing in asp.net MVC, how do I mock a page request?

How do I mock a page request for a .net MVC page?
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
8
votes
1 answer

NSubstitute intercepting "setter" only property invocation

With NSubstitute, is there any way to capture the value you pass to a property setter? E.g. if I have the following interface: public interface IStudent { int Id { set; } string Name { set; } } The say I have a substitute created e.g: var…
bstack
  • 2,466
  • 3
  • 25
  • 38
8
votes
1 answer

How can a Nest Bull queue be tested via Jest (DI via @InjectQueue)?

Given an Injectable that uses a queue via the @InjectQueue decorator: @Injectable() export class EnqueuerService { constructor ( @InjectQueue(QUEUE_NAME) private readonly queue: Queue ) { } async foo () { return…
Jon Lauridsen
  • 2,521
  • 5
  • 31
  • 38
8
votes
3 answers

C# Mocking Framework that supports optional parameters and .Net 4.0

Is there a mocking framework that exists for C# that supports .Net 4.0 and C# completely. Specifically, I'm looking for it to support optional parameters.
Mark S.
  • 1,516
  • 1
  • 16
  • 26
8
votes
1 answer

Why does mock patch only work when running specific test and not whole test suite?

I'm using Django and Pytest specifically to run the test suite and am trying to test that a specific form shows up with expected data when a user hits the site (integration test). This particular view uses a stored procedure, which I am mocking…
Hanny
  • 580
  • 3
  • 16
  • 44
8
votes
2 answers

Jest manual mocks does not use mock file in CRA

I have a CRA app and am trying to the use the manual mocks feature of Jest, but my manual mocks does not seem to be applied. I use a base url of src (by setting it in tsconfig.json). Simplified, I have and have a file structure of src/ |-…
Johan Book
  • 153
  • 1
  • 3
  • 10
8
votes
2 answers

Pytest: Mock multiple calls of same method with different side_effect

I have a unit test like so below: # utilities.py def get_side_effects(): def side_effect_func3(self): # Need the "self" to do some stuff at run time. return {"final":"some3"} def side_effect_func2(self): # Need…
user1452759
  • 8,810
  • 15
  • 42
  • 58
8
votes
4 answers

Mocking jsonwebtoken module with Jest

I try to mock with jest the verify function of the npm module jsonwebtoken. The function return a decoded token but i want to pass a custom return of this function my unit test. I made express request that check the validity of access tokent before…
Seyrinian
  • 103
  • 1
  • 1
  • 3