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

Why would I select Moles as my mocking framework?

I've been looking at several Mocking frameworks for ASP.NET and came across Microsoft Moles. This seems to be a part of Microsoft Research team and was wondering If anyone here has selected Moles over other matured Mocking frameworks such as Moq.
user300981
  • 1,423
  • 5
  • 13
  • 16
9
votes
2 answers

Changing the second result of a function call with mock

I have a loop that looks like this: for i in range(len(some_list)): response = requests.post(some_url, some_params) if response.status_code != HTTPOk: # do something What I would like to do is change response of requests.post in the…
TJ1S
  • 528
  • 3
  • 12
9
votes
1 answer

Hybris Mockito : Getting Exception there is no LocaleProvider for (detached) model

I am writing test cases at facade level in my Hybris Project. I am creating model instance and setting name and code. Model is having some attributes Localized, because of that I am getting no LocaleProvider…
Free-Minded
  • 5,322
  • 6
  • 50
  • 93
9
votes
2 answers

Throw exception after first call

I have a loop where I handle adding records to a zip file. I have mocked my zipfile object and I want to raise a exception to verify that my logic for handling large zipfiles will work correctly. is there a way with MagicMocks or plain mocks to…
Nathan Tregillus
  • 6,006
  • 3
  • 52
  • 91
9
votes
3 answers

Mocking a method outside of a class

I need to write a unit test for credential checking module looks something like below. I apologize I cannot copy the exact code.. but I tried my best to simplify as an example. I want to patch methodA so it returns False as a return value and test…
James C.
  • 501
  • 2
  • 7
  • 16
9
votes
2 answers

Problems trying to mock a Model within Flask-SQLAlchemy

I'm testing a Flask application that have some SQLAlchemy models using Flask-SQLAlchemy and I'm having some problems trying to mock a few models to some methods that receive some models as parameters. A toy version of what I'm trying to do is like…
Rafael S. Calsaverini
  • 13,582
  • 19
  • 75
  • 132
9
votes
2 answers

How do I mock a class in a Python unit test?

I have a class: class A: __init__(self): self.b = B() def is_authorized(self) name = self.b.get_name() if name == 'admin': return True else: return False I'd like to write a unit test to test…
nickponline
  • 25,354
  • 32
  • 99
  • 167
9
votes
1 answer

Groovy metaClass fails when overriding method called in constructor?

I just tried to write this simple code to test overriding methods using metaClass. The code is here: class Hello { public Hello() { Foo() } public void Foo() { println "old" } } It has a Foo()…
danielZ
  • 93
  • 5
9
votes
2 answers

Can you store an 'It' matcher object as a variable, instead of defining it inline? (Moq)

I'm mocking some objects using Moq, and some of them can have fairly lengthy parameter-collection request objects passed in as arguments. For ease of re-use and style, I'd like to be able to store these requests, with specific arguments, for use in…
9
votes
1 answer

For Func, where A extends T, A does not satisfy for T

Okay, let me set the scene: We have a function used within our code that takes a function and does some logging around it and then returns the result. It looks a little something like this. TResponse LoggedApiCall(Func
Kira Namida
  • 279
  • 3
  • 16
9
votes
6 answers

Mocking WebService consumed by a Biztalk Request-Response port

I'm using BizUnit to unit-tests my Biztalk orchestrations, but some orchestrations consume a WebService,and testing these seems more like integration testing than unit testing. I'm familiar with using a mocking framework to mock the generated proxy…
Maxime Labelle
  • 3,609
  • 2
  • 27
  • 48
9
votes
2 answers

How can I mock this asynchronous method?

I have a class that roughly looks like this: public class ViewModel { public ViewModel(IWebService service) { this.WebService = service; } private IWebService WebService{get;set;} private IEnumerable MyData{get;set;} …
Charlie
  • 10,227
  • 10
  • 51
  • 92
9
votes
1 answer

The right way to use MOQ setup and returns

Im new to MOQ and I am a little confused with the setup method. The example below shows one method that i need to test. The method under test returns the latest time from two dates, so I create two datetime objects and pass them to my function. The…
Jed I
  • 998
  • 3
  • 19
  • 37
9
votes
1 answer

AutoMockContainer with support for automocking classes with non-interface dependencies

I have a constructor which has a non-interface dependency: public MainWindowViewModel(IWorkItemProvider workItemProvider, WeekNavigatorViewModel weekNavigator) I am using the Moq.Contrib automockcontainer. If I try to automock the…
Marius
  • 9,208
  • 8
  • 50
  • 73
9
votes
3 answers

Unit Test Web Api 2 Mock User

I'm trying to mock the User.Identity in my Api Controller test. This is my api method: [Route(Urls.CustInfo.GetCustomerManagers)] public HttpResponseMessage GetCustomerManagers([FromUri]int groupId = -1) { var user =…
ozstudent
  • 381
  • 1
  • 5
  • 14