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

Is a Cache mock called more than once when browser-testing?

I'm trying to cover the following: I'm using the following test code: public function test_it_deletes_a_patient() { // ... $cacheKey = vsprintf('%s.%s', [$this->doctorUser->id, 'backoffice.stats.patientsTotalCount']); …
alariva
  • 2,051
  • 1
  • 22
  • 37
8
votes
2 answers

Jest mock function doesn't work while it was called in the other function

I believe something fundamentally wrong in my understanding for Javascript. In file abc.js, I have code export function returnBoolean() { return true; } export function output() { return returnBoolean(); } In the test, I do import * as abc…
KunYu Tsai
  • 632
  • 1
  • 10
  • 15
8
votes
1 answer

Mock the result of accessing public GCS bucket

I have the following code: bucket = get_bucket('bucket-name') blob = bucket.blob(os.path.join(*pieces)) blob.upload_from_string('test') blob.make_public() result = blob.public_url # result is `
Rodrigo
  • 135
  • 4
  • 45
  • 107
8
votes
2 answers

Lite Python AMQP server implementation for dev and mock?

In Django, running ./manage.py runserver is really nice for dev, avoiding the hassle to setup and start a real webserver. If you are not running Django, you can still setup a gunicorn server very easily. Is there something similar for AMQP? I don't…
Bite code
  • 578,959
  • 113
  • 301
  • 329
8
votes
1 answer

Python mock.patch autospec a class with properties

I have a class that defines some properties with the @property decorator. When I patch that class and set autospec=True, i expect the property to behave as as it is spec'd class MyClass(object): def __init__(self): …
Lundy
  • 663
  • 5
  • 19
8
votes
2 answers

Python SQLAlchemy - Mocking a model attribute's "desc" method

In my application, there is a class for each model that holds commonly used queries (I guess it's somewhat of a "Repository" in DDD language). Each of these classes is passed the SQLAlchemy session object to create queries with upon construction.…
rr.
  • 6,484
  • 9
  • 40
  • 48
8
votes
1 answer

Wiremock not matching regex

I'm using wiremock to mock certain requests and their respective response, but I'm trying to add a regex. Unfortunately, this just throws an exception stating that the request was not matched. { "request" : { "method": "GET", …
masha.knezevic
  • 121
  • 1
  • 1
  • 8
8
votes
4 answers

How do I generate a custom message from a custom Mockito ArgumentMatcher?

I'm writing an ArgumentMatcher and the guts of the comparison come down to something like: return A.value().equals(B.value()) && A.name().equals(B.name()); Unfortunately, when the doesn't pass, Mockito just tells me it failed. I want to add a…
Todd R
  • 18,236
  • 8
  • 31
  • 39
8
votes
1 answer

Jest - How to get coverage for mocked classes and implementations

I'm currently working on a project where I'm using Jest for unit testing and code coverage. Everything is working fine, except coverage for mocked classes/methods. I don't seem to get the desired coverage results. I've tried to find something in the…
8
votes
5 answers

NSubstitute Error UnexpectedArgumentMatcherException

I'm getting the following error: NSubstitute.Exceptions.UnexpectedArgumentMatcherException: 'Argument matchers (Arg.Is, Arg.Any) should only be used in place of member arguments. Do not use in a Returns() statement or anywhere else outside of…
user9530048
8
votes
2 answers

Removing previously defined expectations in JMockit

I have an object that I'm mocking with a JMockit NonStrictExcpection() in the @Before/setUp() method of my test class so that it returns the value expected for normal execution of my class under test. This is fine for all of my test methods save for…
chrisbunney
  • 5,819
  • 7
  • 48
  • 67
8
votes
2 answers

What are the advantages to wrapping system objects (File, ServiceController, etc) using the Adapter pattern vs. detouring for unit testing?

Consider the following method that stops a service: Public Function StopService(ByVal serviceName As String, ByVal timeoutMilliseconds As Double) As Boolean Try Dim service As New ServiceController(serviceName) Dim timeout As…
Matt
  • 14,353
  • 5
  • 53
  • 65
8
votes
3 answers

Mocking a method with throw() specifier

I am trying to Google mock a virtual method which has a throw() specifier. The original function looks like this: virtual ReturnValue FunctionName() const throw(); I am getting the compiler error: looser throw specifier for 'virtual…
J R
  • 130
  • 1
  • 5
8
votes
1 answer

moto not mocking ec2?

I'm trying to test some python code that uses boto. I'd rather not try and do an integration test against AWS, so I am trying to mock it out with moto, and it's not behaving as I'd expect. Here's the test code: import io import boto3 from moto…
Geoffrey Wiseman
  • 5,459
  • 3
  • 34
  • 52
8
votes
3 answers

Retrofit 2 mock best practice

I am very new to testing and TDD and I decided to use use Retrofit2-Mock for my api mocking needs. The documentation on Mock Retrofit2 is virtually non existent and the only how-to resources that I found is this article from 2015 and this answer…
saiedmomen
  • 1,401
  • 16
  • 33
1 2 3
99
100