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)

3
votes
2answers
182 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
54 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
25 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
151 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
86 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
167 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
148 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
282 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 ...
4
votes
1answer
98 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
248 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
220 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
275 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
52 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
173 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
610 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
224 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
337 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
0answers
57 views

when would you write static method as it avoid a “seam” for testing [duplicate]

when would you write static method. I used to write it in utils classes where all the methods are static and work state-lessly on arguments only. But now I know it blocks from creating a seam in ...
0
votes
1answer
105 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
289 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 ...
0
votes
0answers
110 views

Populate faked context for unit testing WPF ViewModels using entity framework

We're developing an entity framework based application which has about 20 different entities, with quite a lot of connections of various types (one-many, many-many etc) between them. Each entity also ...
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
365 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
134 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
76 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
137 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
123 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
736 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
145 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
290 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
164 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? ...
2
votes
2answers
2k 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
270 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
469 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
3k 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
144 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 ...
5
votes
6answers
2k 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
278 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 ...
3
votes
3answers
3k 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 ...
6
votes
3answers
3k views

How to mock method with hard coded object?

I am working on an application which have multiple layers. Data access layer to retrieve and save data from data source, business logic to manipulate data, user interface to show the data on screen. ...
2
votes
1answer
2k views

How to unit test a web client?

I am having a lot of trouble understanding how to unit test my web client. I have just finished a project using TDD for the first time - this project hd no external dependencies such as API calls or ...
2
votes
5answers
151 views

Should mock objects for tests be created at a high or low level

When creating unit tests for those other objects, what is the best way to create mock objects that provide data to other objects. Should they be created at a 'high level' and intercept the calls as ...
6
votes
1answer
348 views

How much mocking is “just right?”

I titled the question jokingly because I'm sure that "it depends," but I have some specific questions. Working in software that has many deep layers of dependency, my team has become accustomed using ...
0
votes
1answer
211 views

Method visibility for testing partial mocks

I'm currently writing unit tests to test behavior of a method and would like to partially mock the methods calling injected properties. For example: public void doSomething() { int complicatedInt ...
2
votes
1answer
368 views

Mocking complex data operations in integration tests

We have some problems figuring out how to mock complex data and operations on it. First the problems with generating complex data: In one of our components we have really complex data structures ...
0
votes
2answers
638 views

when to mock in unit testing [duplicate]

I am trying to write unit tests in javascript for an application made in nodejs. What I want to know is - Say there is a class Vehicles which is dependent on another class Roads. Should I mock the ...
4
votes
2answers
460 views

What is the most appropriate testing method in this scenario?

I'm writing some Objective-C apps (for OS X/iOS) and I'm currently implementing a service to be shared across them. The service is intended to be fairly self-contained. For the current functionality ...
3
votes
1answer
127 views

Stubbing and mocking boundaries

Suppose I'm building a JSON API and would like to include an endpoint that returns a list of recently created posts. Somewhere in my system is an existing Post model with the following test: create ...