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
9
votes
4 answers

How to mock System.Data.Linq.Table

One of my base repository classes contains a method: public abstract class RepositoryBase : IRepository where T : IEntity where TDb : class, IDbEntity, new() { protected internal abstract Table GetTable(); ... } I am…
Budda
  • 18,015
  • 33
  • 124
  • 206
9
votes
5 answers

How to avoid duplicate code when using mocks in unittests

I am using dependency injection to supply mocks for code outside of my class under test. I find myself writing alot of the same code over and over as I need to mock out AuthProvider, ConfigurationManager, etc. which are used in the method I want to…
Fadeproof
  • 3,038
  • 5
  • 31
  • 42
9
votes
2 answers

mock xmlrpc.client method python

I am in the process of learning unit testing, however I am struggling to understand how to mock functions for unit testing. I have reviewed many how-to's and examples but the concept is not transferring enough for me to use it on my code. I am…
user1601716
  • 1,893
  • 4
  • 24
  • 53
9
votes
4 answers

Angular 1.5 unit testing component while ignoring child components

I am trying to write a unit test for a component in Angular 1.5. I want to unit test that component and its dom nodes. This component contains a child component that is quite complex. My goal is to unit test the outer component without compiling the…
Pascal Chorus
  • 360
  • 4
  • 12
9
votes
1 answer

patch function with same name as module python django mock

|-- my_module | |-- __init__.py | |-- function.py `-- test.py in function.py: import other_function def function(): doStuff() other_function() return in __init__.py from .function import function in my test.py from django.test…
v.thorey
  • 1,997
  • 1
  • 18
  • 22
9
votes
2 answers

Verify method calls using with different state of object using Moq

Recently I discovered quite interesting behaviour of Moq library (4.5.21) in one of mine C# projects. Below is the class that I am trying to test. public class Order { public string State { get; set; } } public interface IOrderService { …
user6810960
  • 153
  • 4
9
votes
2 answers

Thread-safe version of mock.call_count

It appears that Mock.call_count does not work correctly with threads. For instance: import threading import time from mock import MagicMock def f(): time.sleep(0.1) def test_1(): mock = MagicMock(side_effect=f) nb_threads = 100000 …
julienc
  • 19,087
  • 17
  • 82
  • 82
9
votes
4 answers

MOQ 4.0: The type initializer for 'Moq.Mock`1' threw an exception

I'm getting the exception The type initializer for 'Moq.Mock`1' threw an exception. using Moq 4.0 I've checked around on a couple of forums and they allude to using the Moq-NoCastle version. I've tried both this and version in the Moq folder. …
BenAlabaster
  • 39,070
  • 21
  • 110
  • 151
9
votes
4 answers

Mock objects in C++

What are Mock objects? Can you please explain the concept? How to make use of Mock objects in C++? Any source examples will be highly helpful.
Alok Save
  • 202,538
  • 53
  • 430
  • 533
9
votes
2 answers

Mock_open CSV file not getting any data

I am trying to unit test a piece of code: def _parse_results(self, file_name): results_file = open(file_name) results_data = list(csv.reader(results_file)) index = len(results_data[1])-1 results_file.close() return…
draB1
  • 63
  • 2
  • 7
9
votes
3 answers

When and when not to stub/mock a test

I am making a concerted effort to wrap my head around Rspec in order to move towards more of a TDD/BDD development pattern. However, I'm a long way off and struggling with some of the fundamentals: Like, when exactly should I be using mocks/stubs…
aaronrussell
  • 9,389
  • 5
  • 38
  • 62
9
votes
2 answers

How to test Models in Django with Foreign Keys

I want to make sure I am testing Models/Objects in isolation and not as one huge system. If I have an Order object and it has Foreign Keys to Customers, Payments, OrderItems, etc. and I want to test Order functionality, I need to create fixtures for…
zenWeasel
  • 1,889
  • 2
  • 22
  • 27
9
votes
2 answers

mockito verify method call inside method

for a unit tests i am trying to verify if there is a way to verify a method call inside a method with mockito verify? An example would be: public delete(param) { VideoService.deleteVideo(param); << i want to verify the call of this method …
Tvt
  • 223
  • 1
  • 3
  • 16
9
votes
3 answers

Testing POST route with anti-forgery and ring-mock

I want to write a test for a simple POST request using ring.mock - something like this: (testing "id post route" (let [response (app (mock/request :post "/" {:id "Foo"}))] (is (= 302 (:status response))))) However, since I use the…
Robert
  • 121
  • 6
9
votes
2 answers

What is the most complete mocking framework for HttpContext

I'm looking for as comprehensive as possible of a mock replacement and wrapper for the ASP.NET HttpContext in my applications. A comprehensive mock replacement could potentially increase the testability of my ASP.NET web applications substantially,…
kbrimington
  • 25,142
  • 5
  • 62
  • 74