www.orbitone.com
Raas van Gaverestraat 83
B-9000 GENT, BELGIUM
E-mail info@orbitone.com
Website www.orbitone.com
Tel. +32 ...
28 April, 2010
Moles2
What
Moles can be used to detour any .NET method,
including non-virtual and static methods in seale...
28 April, 2010
Moles3
Why
Unit testing is great, but most free isolation frameworks require
that your mocks implement an ...
28 April, 2010
Moles4
How
It uses runtime instrumentation to inject callbacks in the
method MSIL bodies of the moled meth...
28 April, 2010
Moles5
When
Refactoring is the preferred method of isolation, because it
does not require runtime instrume...
28 April, 2010
Moles6
How to install ?
Visual Studio 2010 Moles - Isolation Framework for .NET
(Free)
http://visualstudi...
28 April, 2010
Moles7
How to install – unexpected part 2
Everyting works fine except the part where I want to prepare
my ...
28 April, 2010
Moles8
Getting Started
If this is your first time with Moles, try the short Exploring Code
With Moles tuto...
28 April, 2010
Moles9
How to add moles to a project ?
Add reference to project
Add Moles Assembly with a single click.
T...
28 April, 2010
Moles10
filtering mechanism in .moles files
We do not always want all types to be moled, this is made
poss...
28 April, 2010
Moles11
How does it look in visual studio
28 April, 2010
Moles12
NUnit
Since Moles needs the Pex profiler to work, you have to run this
NUnit test using Pex comman...
28 April, 2010
Moles13
Limitations
The current implementation of Moles has several limitations.
These limitations might b...
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...
28 April, 2010
Moles15
Stub Types.
Stub Types. A stub of the type T provides a default
implementation of each virtual mem...
28 April, 2010
Moles16
Mole Types
A mole of type T can provide an alternative implementation for
each non-abstract member...
28 April, 2010
Moles17
Delegates
Both stub types and mole types allow using delegates to
dynamically customize the behavi...
28 April, 2010
Moles18
Stubs - Lightweight Test Stubs for .NET
Stubs is a lightweight framework for .NET that provides te...
28 April, 2010
Moles19
Naming Conventions
 Types
Methods
Properties
Interfaces
Foo -> MFoo
void Foo(string v) -> FooStr...
28 April, 2010
Moles20
Moling Interfaces
 The moles generator will create stub types for any .net interface
 public inte...
28 April, 2010
Moles21
No dependency injection necessary
public class OrderService : IOrderService
{
public int AddOrder(O...
28 April, 2010
Moles22
Demos
Some simple examples in visual studio 2010
28 April, 2010
Moles23
Resources
 http://research.microsoft.com/en-us/projects/moles/
 http://martinfowler.com/articles/...
www.orbitone.com
24 Moles
28 April, 2010
Upcoming SlideShare
Loading in...5
×

Mocking the unmockable with Moles

3,600

Published on

Moles is a mocking and stubbing framework developed by Microsoft Research. This presentation looks into the features and possibilities of Moles

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,600
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
37
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Mocking the unmockable with Moles

  1. 1. www.orbitone.com Raas van Gaverestraat 83 B-9000 GENT, BELGIUM E-mail info@orbitone.com Website www.orbitone.com Tel. +32 9 330 15 00 VAT BE 456.457.353 Bank 442-7059001-50 (KBC) Presenter: Wim Roegiers 28 April, 2010 Moles
  2. 2. 28 April, 2010 Moles2 What Moles can be used to detour any .NET method, including non-virtual and static methods in sealed types.
  3. 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. 4. 28 April, 2010 Moles4 How It uses runtime instrumentation to inject callbacks in the method MSIL bodies of the moled methods.
  5. 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. 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. 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. 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. 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. 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 :
  11. 11. 28 April, 2010 Moles11 How does it look in visual studio
  12. 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. 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. 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. 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. 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. 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. 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.
  19. 19. 28 April, 2010 Moles19 Naming Conventions  Types Methods Properties Interfaces Foo -> MFoo void Foo(string v) -> FooString string Value {get;} -> ValueGet IFoo -> SIFoo
  20. 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. 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; };
  22. 22. 28 April, 2010 Moles22 Demos Some simple examples in visual studio 2010
  23. 23. 28 April, 2010 Moles23 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.aspx  http://geekswithblogs.net/thomasweller/archive/2010/04/28/mocking-the-unmockable- using-microsoft-moles-with-gallio.aspx
  24. 24. www.orbitone.com 24 Moles 28 April, 2010
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×