Post on 14-May-2015
description
transcript
www.orbitone.com
Moles
Raas van Gaverestraat 83B-9000 GENT, BELGIUM E-mail info@orbitone.com Website www.orbitone.com
Tel. +32 9 330 15 00VAT BE 456.457.353Bank 442-7059001-50 (KBC)
Presenter: Wim Roegiers28 April, 2010
28 April, 2010Moles2
What
Moles can be used to detour any .NET method, including non-virtual and static methods in sealed types.
28 April, 2010Moles3
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) .
28 April, 2010Moles4
How
It uses runtime instrumentation to inject callbacks in the method MSIL bodies of the moled methods.
28 April, 2010Moles5
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
28 April, 2010Moles6
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
28 April, 2010Moles7
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.
28 April, 2010Moles8
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
28 April, 2010Moles9
How to add moles to a project ?
Add reference to projectAdd 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”)
28 April, 2010Moles10
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 :
28 April, 2010Moles11
How does it look in visual studio
28 April, 2010Moles12
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:\TOOLS\nunit-console.exe Parser.UnitTests.dll
28 April, 2010Moles13
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.
28 April, 2010Moles14
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.
28 April, 2010Moles15
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.
28 April, 2010Moles16
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.
28 April, 2010Moles17
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.
28 April, 2010Moles18
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.
28 April, 2010Moles19
Naming Conventions
Types
Methods
Properties
Interfaces
Foo -> MFoo
void Foo(string v) -> FooString
string Value {get;} -> ValueGet
IFoo -> SIFoo
28 April, 2010Moles20
Moling Interfaces
The moles generator will create stub types for any .net interface
public interface IFileSystem{
string ReadAllText(string fileName); }
Generates - >
28 April, 2010Moles21
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; };
28 April, 2010Moles22
Demos
Some simple examples in visual studio 2010
28 April, 2010Moles23
Resources
http://research.microsoft.com/en-us/projects/moles/ http://martinfowler.com/articles/mocksArentStubs.html http://research.microsoft.com/en-us/projects/pex/molestutorial.pdf http://research.microsoft.com/en-us/projects/pex/stubstutorial.pdf Rufus meets Jimmy @ http://www.invalidcast.com/post/Moles-Dig-It.aspx http://blogs.msdn.com/kristol/archive/2010/03/07/unit-testing-with-microsoft-moles.asp
x http://geekswithblogs.net/thomasweller/archive/2010/04/28/mocking-the-unmockable-u
sing-microsoft-moles-with-gallio.aspx