Loading…
Transcript

Mocking .NET Without Hurting Its Feelings

class DependenciesHog

{

public string foo

{

get;

set;

}

private void bar(int x)

{

//I do stuff

}

public event BigEvent EventHorizon;

private void SpaceFiller()

{

while (true)

{

LookInteresting();

}

}

}

Why use a Mocking Framework?

Chicago Code Camp

April 29, 2017

Using Hand-Crafted, Artisanal Mocks

John M. Wright

john@wrightfully.com

@wright2tweet

www.wrightfully.com

mock

What can they do?

mockObject.VerifyAllExpectations();

mockObject.AssertWasCalled(x => x.MyMethod());

vs

stub

A rose by any other name

would smell as sweet

work about the same

mock types

bit.ly/mocktalkCCC

© Copyright 2017 John M. Wright

How do they do it?

Per Dev:

Per 5 Build Servers:

A Matter of Syntax

Unconstrained Code Examples

Code Examples (aka: What can you do?)

Moq Syntax

NSubstitute Syntax

Generating Mocks

Code being tested

RhinoMocks Syntax

Unconstrained Frameworks:

Make Your Own Man-In-The-Middle Attack

Constrained Frameworks:

Limitations of Class Inheritance

Constrained Frameworks:

Class Inheritance On-Demand

internal types & members

Mocking Internal Types/Members

AssemblyInfo.cs

[assembly: InternalsVisibleTo("Your.TestAssembly")]

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]

sealed types

Generated Classes:

  • Inherit / Extend Mocked Type
  • Override virtual/abstract members
  • Check for configured stubs/mocks
  • Save details for later verification
  • Take action based on configuration

ICorProfilerInfo::

SetILFunctionBody

Hijacked Code:

  • Can change Behavior of ANY Type/Member
  • Doesn't rely on inheritance
  • Everything gets JIT'd, so anything can be mocked!
  • Injected code will:
  • Check for configured stubs/mocks
  • Save details for later verification
  • Take action based on configuration

Moq Syntax

RhinoMocks Syntax

NSubstitute Syntax

Typemock Syntax

MS Fakes Syntax

Typemock Syntax

MS Fakes Syntax

$ $ $

DateTime.Now

All that, plus:

  • public
  • internal
  • methods
  • properties
  • events
  • static
  • sealed
  • private
  • .NET libs
  • 3rd Party libs (Sharepoint)

wrightfully.com/mocktalk

What can

I expect?

private members

JITCompilationStarted

dynamic

Unconstrained

Constrained

public types & members

My test

value

methods, properties, events

partial

mock

  • Open source
  • No extra software needed
  • Limited commercial support
  • May require design compromises
  • Proprietary (closed source)
  • Some require profiler application / VS plugin
  • Commercial support

class DatabaseConnection

{

}

substitute

More Info @

wrightfully.com/mocktalk

mock

vs

stub

http://mimiandeunice.com/2011/07/19/expectations/

dynamic

mock

static types & members

shim

class AnotherOne

{

}

Using A Mocking Framework

Custom Mocks

File I/O

$$$

UI

$$

Integration

$

Unit Tests

FREE

158 members!

partial

Unconstrained

class DatabaseConnection

{

}

Great for testing "legacy" code.

Best fit for code written with testing in mind.

Class Being Tested

interfaces

fake

Tests

virtual / abstract members

strict

Constrained

Moq Syntax

RhinoMocks Syntax

NSubstitute Syntax

Typemock Syntax

MS Fakes Syntax

strict

mock

class DependenciesHog

{

public string foo

{

get;

set;

}

private void bar(int x)

{

//I do stuff

}

public event BigEvent EventHorizon;

private void SpaceFiller()

{

while (true)

{

LookInteresting();

}

}

}

Moq Syntax

RhinoMocks Syntax

NSubstitute Syntax

Typemock Syntax

MS Fakes Syntax