3. 28 April, 2010
Moles3
Why
Unit testing is great, but most free isolation frameworks require
that your mocks implement an interface.
But what do you do when the class you are trying to mock is
static or sealed with no interface ?
Moles, supports mocking almost any CLR based class (including
sealed and static classes) .
4. 28 April, 2010
Moles4
How
It uses runtime instrumentation to inject callbacks in the
method MSIL bodies of the moled methods.
5. 28 April, 2010
Moles5
When
Refactoring is the preferred method of isolation, because it
does not require runtime instrumentation.
However, refactoring is often not a choice—in which case,
runtime instrumentation might be the only solution.
e.g. with code that calls into the .NET framework, some third-
party or legacy stuff etc
6. 28 April, 2010
Moles6
How to install ?
Visual Studio 2010 Moles - Isolation Framework for .NET
(Free)
http://visualstudiogallery.msdn.microsoft.com/en-
us/b3b41648-1c21-471f-a2b0-f76d8fb932ee
Latest version : v0.91.50418.0, 04/19/2010
7. 28 April, 2010
Moles7
How to install – unexpected part 2
Everyting works fine except the part where I want to prepare
my Stubs and Moles. You need to create a new .moles file based
upon a new template called Moles and Stubs for Testing. the
only issue was … the template was nowhere to be found !
Solution :
It takes a while but after a few moments it is completed, now
open Visual Studio 2010 again and select your Moles and Stubs
for Testing template.
8. 28 April, 2010
Moles8
Getting Started
If this is your first time with Moles, try the short Exploring Code
With Moles tutorial.
Also try the short Unit Testing with Moles tutorial.
To get an overview, read Getting Started with Moles.
Also useful
http://blogs.msdn.com/kristol/archive/2010/03/07/unit-
testing-with-microsoft-moles.aspx
9. 28 April, 2010
Moles9
How to add moles to a project ?
Add reference to project
Add Moles Assembly with a single click.
The moles are generated, and a new dll is added to the
references.
Add atribute to the tests to detour to the moled dll’s :
HostType(“Moles”)
10. 28 April, 2010
Moles10
filtering mechanism in .moles files
We do not always want all types to be moled, this is made
possible with a filtering mechanism.
You can define a filter in the .mole file :
12. 28 April, 2010
Moles12
NUnit
Since Moles needs the Pex profiler to work, you have to run this
NUnit test using Pex command line runner with NUnit console.
Example:
pex /runner:D:TOOLSnunit-console.exe Parser.UnitTests.dll
13. 28 April, 2010
Moles13
Limitations
The current implementation of Moles has several limitations.
These limitations might be resolved in future releases :
The Moles framework supports only a limited number of
method signature—up to 10 arguments, where the last
argument can be an out or ref argument.
Method signatures with pointers are not supported.
14. 28 April, 2010
Moles14
Mocks and Stubs
A stub can never fail a test.
A mock records things and then tells our test if it's expectations
have been met according to its recording.
15. 28 April, 2010
Moles15
Stub Types.
Stub Types. A stub of the type T provides a default
implementation of each virtual member of T—that is, virtual or
abstract methods, properties, and events. The default behavior
can be dynamically customized for each member
Although stub types can be generated for interfaces and non-
sealed classes with overridable methods, they cannot be used
for static or non-overridable methods.
16. 28 April, 2010
Moles16
Mole Types
A mole of type T can provide an alternative implementation for
each non-abstract member of T. The Moles framework will
redirect method calls to members of T to the alternative mole
implementation.
17. 28 April, 2010
Moles17
Delegates
Both stub types and mole types allow using delegates to
dynamically customize the behavior of individual stub
members.
Moles is a very lightweight framework; it does not provide
advanced declarative verification features found in other mock
frameworks, such as Moq, Rhino Mocks, NMock, or Isolator.
18. 28 April, 2010
Moles18
Stubs - Lightweight Test Stubs for .NET
Stubs is a lightweight framework for .NET that provides test
stubs.
For interfaces and non-sealed classes, type-safe wrappers are
generated that can be easily customized by attaching delegates.
Stubs are part of Moles.
20. 28 April, 2010
Moles20
Moling Interfaces
The moles generator will create stub types for any .net interface
public interface IFileSystem
{
string ReadAllText(string fileName);
}
Generates - >
21. 28 April, 2010
Moles21
No dependency injection necessary
public class OrderService : IOrderService
{
public int AddOrder(Order order)
{
// Save order using Order Repository
var repository = new OrderRepository();
return repository.SaveOrder(order);
}
[TestMethod]
[HostType("Moles")]
public void CanAddOrderWithoutMole()
{
MOrderRepository.AllInstances.SaveOrderOrder = (r, o) =>
{
// Increase counter to be sure of how many times
// our implementation of SaveOrder gets called
callesToSaveOrder++;
// Customer name should be the one we provided
Assert.AreEqual("Test Customer", o.CustomerName);
// Order date should be the 2010-03-07 14:20:00, because
// we just replaced the implementation of DateTime.Now
Assert.AreEqual(new DateTime(2010, 03, 07, 14, 20, 0), o.OrderDate);
// Allways return 42 as OrderID
return 42;
};