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
1 answer

Python2.7 contextlib.ExitStack equivalent

To programmatically combine context managers I use the following code: == helpers.py == from contextlib import nested import mock def multiple_patch(obj_to_be_patch, *methods): return nested( *[mock.patch.object(obj_to_be_patch, method)…
Ali SAID OMAR
  • 6,404
  • 8
  • 39
  • 56
9
votes
3 answers

How do I prevent my unit tests from requiring knowledge about implementation internals when using mock objects?

I'm still in the learning stages regarding unit-testing and in particular regarding mocking (I'm using the PascalMock and DUnit frameworks). One thing I now stumbled over was that I couldn't find a way around hard-coding implementation details of…
Oliver Giesen
  • 9,129
  • 6
  • 46
  • 82
9
votes
2 answers

how to deal with the "fmt" golang library package for CLI testing

Disclaimer: I wish you a merry XMas and I hope my question does not disturb you! sample.go: package main import( "fmt" "os" ) type sample struct { value int64 } func (s sample) useful() { if s.value == 0 { …
moin moin
  • 2,263
  • 5
  • 32
  • 51
9
votes
3 answers

Mocking a method that returns dynamic return type with Moq

Given the following interface: public interface IApiHelper { dynamic CallApi(string url); } I've delclared an instantiated a Mock _apiHelperMock I'm trying to write a test that returns a Success = true property, to mimic a JSON…
Marcus K
  • 779
  • 1
  • 10
  • 22
9
votes
2 answers

Mocking delegates with Moq

I have an interface: public interface IRepeater { void Each(string path, Action action); } I want to mock this interface using Moq. Now I can obviously do the following: var mock = new Mock(); mock.Setup(m =>…
baynezy
  • 6,493
  • 10
  • 48
  • 73
9
votes
4 answers

How to mock JSONObject?

In the middle of a method I'm doing something like this JSONObject = new JSONObject(json); Now I'm trying to unit test that using Android Studio and JUnit. Unfortunately, Android Studio won't let me create a real JSONObject instance and ask me to…
StackOverflower
  • 5,463
  • 13
  • 58
  • 89
9
votes
1 answer

unittesting sqlalchemy BinaryExpressions

I'm writing some unittests for some code that uses SQLAlchemy. I want to test filter calls, but it seems that SQLAlchemy BinaryExpression objects created with the same arguments don't compare equal: AssertionError: Expected call:…
swizzard
  • 1,037
  • 1
  • 12
  • 28
9
votes
2 answers

How does @mock.patch know which parameter to use for each mock object?

Looking at this webpage: http://www.toptal.com/python/an-introduction-to-mocking-in-python -- The author talks about Mocking and Patching in Python and gives a pretty solid "real-world" example. The part that is tripping me up is understanding how…
Pretzel
  • 8,141
  • 16
  • 59
  • 84
9
votes
5 answers

Mock with Mockito return a InvocationTargetException

I'm writing some unit test and I stumble accross this strange "bug" which prevent me from running my unit test. When I run the "when(...).thenReturn(...)", I receive a InvocationTargetException. Then strange things is that when I debug, it goes…
Jean-François Côté
  • 4,200
  • 11
  • 52
  • 88
9
votes
2 answers

Python testing using mock library for user input in a loop

I am trying to use mock library for testing a piece of the code. In this code, the user raw input is accepted in a for loop as shown below. I have written the test case test_apple_record that can feed a single user input value for the tray number.…
stackjs
  • 443
  • 2
  • 6
  • 13
9
votes
1 answer

Mock with submodules for ReadTheDocs

I'm trying to document a Python project with ReadTheDocs. Initially, the build process would die when it got to: from osgeo import gdal, osr I've read the rtd faq and used mock for the osgeo module that was giving me trouble. Now the build process…
jkibele
  • 408
  • 4
  • 12
9
votes
2 answers

expected: 1 time with any arguments received: 0 times with any argument

I'm testing the index action for my ProjectsController. I'm using the will_paginate gem, and am trying to write an RSpec test that ensures the paginate method is called on the current user's projects when they projects_path. The result I'm getting,…
bitfizzy
  • 157
  • 1
  • 1
  • 8
9
votes
1 answer

How can I mock a method with a return type of unique_ptr in Google Mock?

I have read Can Google Mock a method with a smart pointer return type? but it did not really give much of an answer. I have a factory that returns unique_ptr instances. Returning unique_ptr is a requirement that cannot change without really good…
dawsonc623
  • 1,841
  • 2
  • 16
  • 26
9
votes
2 answers

When to use a Mock v. Stub, or neither?

I've been reading up on Mocks and Stubs, their differences and uses. I'm still a bit confused, but I think I've got the jist of it. Now I'm wondering about applications. I can see the use in creating "fake" objects in testing scenarios where the…
9
votes
1 answer

How to stub a class method using rspec/rspec-mocks

I am using rspec-mock for test-driven-development. I am starting implementing a single class and mocking/stubbing the other classes using rspec-mock. Mocking objects of classes yet to be implemented works well. However when I try to mock a class…
Juergen
  • 115
  • 1
  • 1
  • 5