Mocking .NET Without Hurting Its Feelings (Stack Overflow Team Meetup 2017)

JW
by John Wright0 views·Updated Jun 29, 2017
Powered by AI

Need to present on this topic?

Create your own polished presentation in minutes. Just describe what you need.

Free to try, no credit card required

Slide outline

  1. $ $ $
  2. DateTime.Now
  3. class DatabaseConnection
  4. class AnotherOne
  5. File I/O
  6. $$$
  7. FREE
  8. class DatabaseConnection
  9. class DependenciesHog

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?

Using Hand-Crafted, Artisanal Mocks

John M. Wright

john@wrightfully.com

@wright2tweet

mock

mock

What can they do?

mock.VerifyAllExpectations();

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

mock

vs

stub

vs

stub

A rose by any other name

would smell as sweet

work about the same

www.wrightfully.com

mock types

© Copyright 2017 John M. Wright

How do they do it?

Per Dev:

Per 5 Build Servers:

A Matter of Syntax

Code Examples (aka: What can you do?)

Unconstrained Code Examples

Generating Mocks

Example Test using a Mock

Code being tested

Using Moq Syntax

Code being tested

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

Code Examples (aka: What can you do?)

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
1

Internal Dev Team Meetup

July, 2017

2

Moq Syntax

RhinoMocks Syntax

NSubstitute Syntax

Typemock Syntax

MS Fakes Syntax

3

Typemock Syntax

MS Fakes Syntax

4

$ $ $

5

DateTime.Now

6

Throwing Exceptions

7

All that, plus:

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

Mocking Multiple Calls

9

wrightfully.com/mocktalk

10

Alternate Implementations

11

What can

I expect?

12

private members

13

Creating and Checking Expectations

14

JITCompilationStarted

15

dynamic

16

Unconstrained

Constrained

17

public types & members

18

My test

value

19

Stipulate A Return Value

20

methods, properties, events

21

partial

mock

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

class DatabaseConnection

{

}

24

substitute

25

More Info @

wrightfully.com/mocktalk

26

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

27

dynamic

mock

28

static types & members

29

shim

30

class AnotherOne

{

}

31

Using A Mocking Framework

32

Custom Mocks

33

File I/O

34

$$$

UI

$$

Integration

$

Unit Tests

35

FREE

36

158 members!

37

partial

38

Unconstrained

39

class DatabaseConnection

{

}

40

Great for testing "legacy" code.

Best fit for code written with testing in mind.

41

Class Being Tested

42

interfaces

43

fake

44

Tests

45

Return Value Based On Input

46

virtual / abstract members

47

strict

48

Constrained

49

Moq Syntax

RhinoMocks Syntax

NSubstitute Syntax

Typemock Syntax

MS Fakes Syntax

50

strict

mock

51

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();

}

}

}

52

Moq Syntax

RhinoMocks Syntax

NSubstitute Syntax

Typemock Syntax

MS Fakes Syntax