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

How to set a variable within a mock object

Is there any way to set a class level variable within a mock object? I have the mock object set similar to this: $stub = $this->getMock('SokmeClass', array('method')); $stub->expects($this->once()) ->method('method') …
Enrique
  • 655
  • 1
  • 7
  • 16
9
votes
1 answer

Setting up Moq to ignore a virtual method

I have an abstract class that has a virtual method. The method is virtual in the event that a later implementation needs to override that functionality. However, Moq proxies all virtual methods so I don't seem to be able to test the actual code…
moribvndvs
  • 42,191
  • 11
  • 135
  • 149
9
votes
3 answers

How to mock a QML component

Actually I'm trying to run some test on a QML component which embeds C++ objects. Unfortunately, I'm getting some errors when I execute my tests. The C++ objects aren't recognized by the QML file. That makes also sense as the C++ objects are set in…
lucaboni
  • 2,334
  • 2
  • 29
  • 41
9
votes
2 answers

Mockito: How to test my Service with mocking?

I'm new to mock testing. I want to test my Service method CorrectionService.correctPerson(Long personId). The implementation is not yet written but this it what it will do: CorrectionService will call a method of AddressDAO that will remove some of…
Michael Bavin
  • 3,944
  • 6
  • 31
  • 35
9
votes
2 answers

Mockito swallows up stack trace?

Why does Mockito swallow up stack traces? For example, if I have a public class Foo { public void foo() { bar(); } public void bar() { baz(); } public void baz() { throw new…
ManRow
  • 1,563
  • 4
  • 21
  • 40
9
votes
2 answers

vfsstream paths and realpath

I'm experimenting with vfsStream for unit testing filesystem interactions and have very quickly run into a major hurdle. One of the validation checks the code under test does is execute realpath() on a supplied input path to test that it's an…
GordonM
  • 31,179
  • 15
  • 87
  • 129
9
votes
2 answers

How to mock App::make() on UnitTesting Laravel 4

i got a question when i was unit testing my application. I Have a method that require a dependency but only that method need it so i thought to don't inject it by construct but initialize it with App::make() of the IoC container Class. But now how…
Fabrizio Fenoglio
  • 5,767
  • 14
  • 38
  • 75
9
votes
3 answers

EF6 Mocking derived DbSets

I am trying to apply the new mocking of EF6 to my existing code. I have a class that Extends DbSet. One of the methods call the base class (BdSet) Create method. Here is the a sample code (not the complete solution or real names): public class…
Andre Vianna
  • 1,713
  • 2
  • 15
  • 29
9
votes
2 answers

Entity-Framework. Testing that SaveChanges is present and called in the correct place in a method

I have a class like the following, that I want to unit test: public class AddUserCommand { IDbContext dbContext; public AddUserCommand(IDbContext context) { dbContext = context; } public void Execute() { …
Matt Hyde
  • 1,572
  • 1
  • 13
  • 17
9
votes
1 answer

Using Python Mock library to spy on internal method calls

I'm using the Python mock module for tests. I want to spy on internal method calls made by a live object. I discovered that the 'wraps' kwarg can be used to set up a mock that spies on method calls to a live object: Using Python mock to spy on calls…
NeilenMarais
  • 2,949
  • 1
  • 25
  • 23
9
votes
1 answer

Mocking Python iterables for use with Sphinx

I'm using Sphinx to document a project that depends on wxPython, using the autodocs extension so that it will automatically generate pages from our docstrings. The autodocs extension automatically operates on every module you import, which is fine…
Chris Krycho
  • 3,125
  • 1
  • 23
  • 35
9
votes
2 answers

How to mock DriverManager.getConnection(...)?

I have a class, which connects to an H2 database and runs several SQL statements. public class H2Persistence implements IPersistence { private Connection conn; @Override public void open() { try { …
Glory to Russia
  • 17,289
  • 56
  • 182
  • 325
9
votes
2 answers

How can I remove library code calls from pytest traceback?

I'm using pytest with mock in my python project. When I get a test failure that involves a mock object (almost all of them), the traceback dives into the mock library source code and my code that's actually triggering the failure often scrolls out…
scanny
  • 26,423
  • 5
  • 54
  • 80
9
votes
2 answers

Unable to stub redis in rspec tests

I'm trying to stub redis using the mock_redis gem for my rspec tests. My rspec config looks like this: RSpec.configure do |config| # ... various rspec config options ... config.before(:each) do redis_instance = MockRedis.new …
Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
9
votes
3 answers

Mocking azure blob storage in unit tests

Is there anyway I can mock azure blob storage without running the storage emulator?It would be of great help if someone could shed some light on this
Kaarthik
  • 627
  • 2
  • 12
  • 32