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

Mock server in a Controller

I have the following line in my controller: string lTempPath = Path.Combine(Server.MapPath("~/Temp"), lRandomFileName); Problem is Server is not virtual and can only be accessed with a getter. I get a "The method or operation is not…
willwin.w
  • 174
  • 1
  • 9
9
votes
4 answers

How to unit test with a mocked file object in Python?

I have a class which I instantiate by giving a file name like parser = ParserClass('/path/to/file'), then I call parser.parse() method which opens and reads the file. Now I want to unit test that if something bad happening inside: with…
kissgyorgy
  • 2,947
  • 2
  • 32
  • 54
9
votes
2 answers

Cleanly Mocking Remote Servers and APIs for Django Unittests

I have a thorny problem that I can't seem to get to grips with. I am currently writing unit tests for a django custom auth-backend. On our system we actually have two backends: one the built-in django backend and the custom backend that sends out…
osman
  • 180
  • 3
  • 7
9
votes
10 answers

Which objects to mock when doing TDD

When creating methods, should every object instantiated inside that method be passed in as a parameter so that those objects can be mocked in our unit tests? We have a lot of methods here at work that have no associated unit tests and upon writing…
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
9
votes
2 answers

C# is there a a spying framework like mockito for .NET 3.5?

I used to have a very convenient spying framework in java called Mockito. It allows you to mock some of methods on existing objects and also could tell you if others were called (you'd create a spy wrapper for that). Is there anything like that for…
Artem
  • 7,275
  • 15
  • 57
  • 97
9
votes
3 answers

How does a mocking framework work?

Most mocking frameworks only are capable of mocking interfaces, some can mock virtual methods of classes. Some Java mocking frameworks are even capable of mocking static classes. E.g. Rhino mock: var mock = MockRepository.GenerateMock<..>(); What…
bas
  • 13,550
  • 20
  • 69
  • 146
9
votes
2 answers

Moq It.Is<> not matching

This code: hub.MockedUserRepository.Setup(r => r.Update(It.IsAny())) .Callback((ControllUser usr) => Console.WriteLine("NULL = " + (usr.Zombies[0].ConnectionId == null))) …
ErikTJ
  • 2,001
  • 3
  • 21
  • 38
9
votes
1 answer

How can i inject a mock service into a unit test for a filter?

I have a simple angularjs filter (it takes an id and converts it to a name string), that depends on a custom service to do its work: angular.module('app').filter('idToName', function(User) { return function(id) { var result, user; …
David Bochenski
  • 309
  • 4
  • 11
9
votes
1 answer

Testing practices for Meteor

This isn't so much a question of technology; I see that a lot of people are using Mocha, which looks neat, so I'll try it. What I'm wondering is how people deal with testing things like models that are tied to Meteor collections. Would you use sinon…
Samo
  • 8,202
  • 13
  • 58
  • 95
9
votes
2 answers

does anyone has an example using $httpBackend (service in module ngMockE2E )

I'm really looking to find the simplest way to get my angular app to use a mock backend service. any pointers would be great, a sample app that show how to write a simple app the use it would do the job. tnx!
Or A
  • 1,789
  • 5
  • 29
  • 55
9
votes
3 answers

Mockito - Mocking Concrete Classes

Given the following code: LinkedList list = mock(LinkedList.class); doCallRealMethod().when(list).clear(); list.clear(); by executing this test, a NullPointerException is thrown from first line in LinkedList#clear: public void clear()…
mhshams
  • 16,384
  • 17
  • 55
  • 65
9
votes
2 answers

How to set the IP (UserHostAddress) on a "mocked' BaseHttpContext?

I'm trying to create a dummy BaseHttpContext to do some (mock) testing without having to do an actual request. I like to add an IP address to my dummy context. string url = "http://www.google.com"; //wrap in uri Uri uri = new Uri(url); //create…
Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
9
votes
5 answers

Java `final` class and mocking

I'm developing a programming game where players will have access to an abstract class and extend it to control the behaviour of a robot. Because it is a programming game, I am trying to protect my game infrastructure so that players don't mess with…
Hilikus
  • 9,954
  • 14
  • 65
  • 118
9
votes
2 answers

FakeItEasy - Is it possible to intercept a method and replace it with my own implementation?

I have the following interface : public interface IOuputDestination { void Write(String s); } In my unit test, I mock it like so : var outputDestination = A.Fake(); What I want to do, is intercept the Write method so that…
marco-fiset
  • 1,933
  • 1
  • 19
  • 31
9
votes
5 answers

When to use stubs and mocks?

I've this confusion all the time. If I write a code which uses fake code to assert some operation, how do i trust my real implementation when it is started really using the real objects instead of fake ones. For example, I've this code -- …
asyncwait
  • 4,457
  • 4
  • 40
  • 53