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.

learn more… | top users | synonyms (1)

1
vote
1answer
30 views

Is it possible to mock and inject traits in PHPUnit?

I need to extend a third party class I cannot modify. The class's dependencies are for the most part injected through the constructor, making them easy to mock. However the class also uses methods ...
1
vote
2answers
84 views

Testing using mocking, must I mock all dependencies too?

I have the following method to test: public List<MarkId> getMarkIdList(ICar carDoc) { ICourseCar courseCarDoc = courseCarRep.get(carDoc); List<MarkWag> markWagList = ...
5
votes
1answer
91 views

Unit testing an API client and wrappers

I've been going round in circles trying to figure out the best way to unit test an API client library I'm developing. The library has a Client class which basically has a 1:1 mapping with the API, and ...
4
votes
2answers
99 views

Does one need to mock concrete data objects?

I've been working on a large project where much of the code is not under test. I have been able to add some unit tests to functional areas of the code, and also to start mocking services but I have a ...
-1
votes
1answer
103 views

How mock is different from other test doubles? [closed]

I was reading an article here on Mocks. I would like to conclude to make my understanding more clear. Anything that can be placed in place of a real object while testing is called a Test Double. ...
0
votes
1answer
121 views

TDD - Outside In developemnt

I'm pretty comfortable with the core concepts of Outside In (especially from this question). However one thing eludes me concerning the mocks you created at each level which then lead to creating the ...
0
votes
1answer
149 views

Should I use a mock or create a new instance of an object in unit tests? [closed]

I have to write a unit test for a method like: void doSomethingWith(Country country) {...} There are the following classes: Interface: public interface Country { String getName(); ... // and ...
1
vote
0answers
23 views

Dealing with stubbed external dependency

In sut I should I have two kinds of dependencies to be stubbed: 3rd party dependencies (mongodb & co) and own dependencies: var async = require('async'), // This dependency should not be stubbed ...
-2
votes
3answers
146 views

TDD/BDD: Mocks with expectations: bad? [closed]

Is using mocks in the following way a bad idea: writing tests where mocks expect certain calls from the subject under test Or even just writing mocks that return values to the subject under test ...
3
votes
3answers
180 views

How to test code that depends on complex APIs (Amazon S3 for instance)?

I am struggling with testing a method that uploads documents to Amazon S3, but I think this question applies to any non-trivial API/external dependecy. I've only come up with three potential solutions ...
33
votes
6answers
3k views

How do I test a system where the objects are difficult to mock?

I am working with the following system: Network Data Feed -> Third Party Nio Library -> My Objects via adapter pattern We recently had an issue where I updated the version of the library I ...
3
votes
2answers
403 views

Converting static utility class into singleton

In company where I work we have lots of "utility" classes, each has lots of code inside (thousands of lines), and they are all static. And one static methods call anothers. The problem here is that ...
13
votes
4answers
1k views

Do mocks violate the Open/Closed principle?

Some time ago I read, on a Stack Overflow answer that I can't find, a sentence that explained that you should test public APIs, and the author said that you should test interfaces. The author also ...
1
vote
2answers
105 views

Is the use of a constant as a parameter of an Unit Test mocked dependency's method wrong?

We have a Unit Test Case where we want to assert that after a login operation, the SUT is able to call a method on one of it's dependencies. This dependency is a NavigationManager and the expectation ...
0
votes
1answer
38 views

Should I create mock objects from scratch or modify the result of real calls in weakly typed languages?

In answer to Is testable code better code? I showed how time-dependent code could be testable by using mocks rather than modifying the implementation: def time_of_day(): return ...
5
votes
2answers
222 views

How to setup build system for unit testing + mocking?

We have a legacy code base in entirely C++. Our build system is CMake. My first stab at unit testing was as follows: Define a target (LibraryA). This library contains the code to test. Define a unit ...
0
votes
0answers
253 views

How to verify a method is only called once, with specific arguments (using RhinoMocks)

I'm just starting to use RhinoMocks. Now I'm wondering what test cases I should write. I want to assert that a method is only being called once, with specific arguments. Lets start with my ...
3
votes
2answers
223 views

Domain object model: query by id vs object

Let assume I have two simple model classes: Product and Brand It is obvious I have a query method in Product class like this Product product = Product.findById(123); What if, I want to query ...
4
votes
1answer
213 views

Is contructing complex mocks for unit testing a sign of bad architecture?

The title is fairly self explanatory. To elaborate: I'm currently updating a suite of unit tests for an application which makes heavy use of Entity Framework. We connect to this via a classic ...
1
vote
5answers
344 views

Are mocks in unit tests dangerous in dynamic languages?

I've started relying heavily on a mocking framework in php for my unit tests. My concern is that with a dynamic language, there is no way of enforcing a return type. When mocking, you have to ensure ...
5
votes
1answer
152 views

Cons of using unconstrained frameworks for unit testing

It's very easy to write unit tests for legacy code using unconstrained frameworks, such as TypeMock Isolator. But what about writing unit tests for newly written code? Is it a good practice to use an ...
4
votes
1answer
438 views

How well am I writing my tests?

I've been coding as a career for about 2 years now but am just now writing my first "real" tests for a non-trivial application. I've been trying to follow best practices I've picked up from the ...
3
votes
5answers
255 views

Injecting collections as constructor dependencies

We are using constructor dependency injection in our application. Following that approach we inject everything using an injection container so we are able to replace any dependency with a Mock. Some ...
6
votes
3answers
302 views

Implementing common logic in base class

Background In the documentation of a project I'm working on I came across the following sentence which immediately triggered an alarm for me: when having several concrete classes that inherits ...
1
vote
1answer
57 views

Breaking dependencies on remote APIs

The system I'm working on has a core web application where the bulk of the work is done. However, it relies on another server to handle transactional finance data. It connects to the server using a ...
1
vote
2answers
276 views

Should you mock key-value stores/aggregators

I've always been taught that your tests should run in isolation, and other than a db for ActiveRecord etc., your tests should not rely on any external software/services. e.g. I always mock my other ...
7
votes
6answers
682 views

I'm confused about which is the correct way to work with TDD

I'm trying to grasp what's the idea behind TDD and how a team is supposed to work with it. I have the following test case with NUnit + Moq (just writing by memory, it is not assured the example ...
0
votes
3answers
332 views

What's the idea behind mocking data access in unit tests [duplicate]

I understand that you're not actually supposed to hit the database or disk when doing unit tests... Why is that? Also, taking something like Moq, what is it actually supposed to mock and where? For ...
1
vote
3answers
394 views

How can I mock this architecture?

This is not a very general question, so it may not exactly be appropriate here, but I could sure use a suggestion if you have one: I have an object containing a dictionary keyed off of an enum, ...
0
votes
1answer
106 views

Should I write a unit test to this?

Given the following pseudocode: List dohList = new List(); Foo foo = A.getFoo(); if ( foo != null ) { Bar bar = foo.getBar(); if ( bar != null ) { FooBar fb = bar.getFooBar(); ...
0
votes
1answer
430 views

Create fake HTTP server to simulate an API for testing

I'm looking to mock responses from an external API service that returns JSON during live events. The first response would contain (fake) events that have happened up to time X, the second response ...
6
votes
3answers
1k views

How to unit test code that downloads a file from a github repo

This is my solution's structure: Storage.csproj > FileDownloader.cs > GitHubProvider.cs (implements IStorageProvider) > IStorageProvider.cs Storage.Test.csproj > ...
1
vote
2answers
447 views

Is a mocking framework useful if I'm unit testing a repository/database? [duplicate]

I'm trying to improve my testing skills. I frequently hear about mocking framework to build fake objects (as far as I understand). Right now I need to test my repository (the usual add, remove, update ...
1
vote
1answer
157 views

Using Mockito.reset

I've just read a lot of arguments against using Mockito.reset and can't make much sense of it. Put differently, I agree in general, but there's always a case which looks like using reset is useful or ...
1
vote
1answer
84 views

Question about mocking externals [duplicate]

at company we're developing quite a big project and we're arguing at the testing strategy. The question is: should all of the tests be executed in isolation of external services like database or APIs ...
0
votes
1answer
155 views

How to write a good mock for testing

I code in Python and I'm starting to use the Mock library. Although I've read through all of the documentation, something that I'm finding hard to understand is 'What makes a good mock'. For ...
0
votes
1answer
128 views

How do you detect lying Stubs/Mocks?

Consider this piece of code from the PHPUnit manual(I'm using PHP only as an example): class StubTest extends PHPUnit_Framework_TestCase { public function testStub() { // Create a ...
2
votes
2answers
1k views

How to unit test without mocks and not be tied to a concrete implementations of an interface

I consider myself to be a 'mockist' on the 'mockist' vs 'classicist' debate, but I am trying to understand the other point of view. Imagine I have this class, where a concrete instance of IDependency ...
0
votes
2answers
247 views

Should we write a unit test for class that call another class that have code written

Suppose that there are two class 'A' and 'B' 'A' has a lot of nested conditions that have all unit test covered. 'B' has a property that will call class 'A' and return value according to the result ...
0
votes
4answers
326 views

What does “mockable” mean?

I came across this term when looking into PHP's "traits" (which are apparently bad, since, among things, they're not mockable), but I can't really find a definition for this "mockability". It might be ...
5
votes
3answers
167 views

Using mocks to set up object even if you will not be mocking any behavior or verifying any interaction with it?

When building a unit test, is it appropriate to use a mocking tool to assist you in setting up an object even if you will not be mocking any behavior or verifying any interaction with that object? ...
3
votes
2answers
3k views

Testing C# Eventhandler Subscription

I want my unit tests to be able to verify that some events were subscribed or unsubscribed at specific points. Currently, I see two options. VerifiableEventHandler Each client subscribes using the ...
3
votes
3answers
289 views

how and should I 'unit test' an entire package?

I'm still learning to be good about doing unit level testing, as I've always been a little sloppy about only doing functional testing in the past, so I want to double check I'm doing this 'right'. I ...
16
votes
6answers
1k views

From a TDD perspective, am I a bad person if I test against a live endpoint instead of a mock?

I follow TDD religiously. My projects typically have 85% or better test coverage, with meaningful test cases. I do a lot of work with HBase, and the main client interface, HTable, is a real pain ...
2
votes
4answers
549 views

Test Doubles, Mocks and Stubs - when not to use & why, for Ruby, Rspec

I've learned about mocking and stubbing and I've seen how they can help me create great test suites that run blindingly fast and thus speed up my development process hugely. However I've also seen ...
2
votes
4answers
5k views

Mocking static methods

Having recently returned from a Test Driven Development (TDD) course I had the following thought. While writing unit tests using Mockito we came up against of the problem of mocking static methods. ...
0
votes
2answers
194 views

Using interfaces as part of encapsulation

I'm creating interfaces for a number of our existing classes for mocking reasons. Many of these classes also have package scope methods as an attempt to give some level of encapsulation by ensuring ...
6
votes
6answers
3k views

Are HSQLDB unit tests an anti pattern?

HSQLDB is great. It (also) has an embedded mode (no dedicated server needed), which allows for quick prototyping of stuff like Proof of Concepts, and it can also be great in production-ready ...
1
vote
2answers
333 views

TDD - Making assertions about C# Expression trees

How would you unit test the PerformUpdate method in the sample below? This is a (very) simplified version of some code that performs updates against a database where the schema is dynamic and ...
4
votes
3answers
5k views

How to use mock objects [C++] without passing them as arguments to functions

I'm in the process of integrating a Unit Testing Framework for an existing code base in C++. I've zeroed down on CxxTest, but as it turns out we can use other Mocking Frameworks (like googlemock) in ...