Mocking .NET Without Hurting Its Feelings (Beer City Code)

JW
by John Wright0 views·Updated Jun 23, 2018
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 DatabaseConnection
  5. class AnotherOne
  6. File I/O
  7. $$$
  8. FREE

Transcript

Mocking .NET Without Hurting Its Feelings

June 23, 2018

John M. Wright

john@wrightfully.com

@wright2tweet

www.wrightfully.com

© Copyright 2018 John M. Wright

wrightfully.com/mocktalk

https://www.ftc.gov/news-events/media-resources/truth-advertising/advertisement-endorsements

As per the FTC’s Guides Concerning the Use of Endorsements and Testimonials in Advertising:

Please be aware that I have received a Community license, at no cost to me, for Typemock Isolator, due to involvement in the development community and my writing/speaking on mocking topics.

Legal Disclaimer

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

}

}

}

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?

Custom Mocks

Using Hand-Crafted, Artisanal Mocks

Class Being Tested

mock

test double

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

mock types

How do they do it?

Per 5 Build Servers:

as of 2018-01-22

Code Examples (aka: What can you do?)

Code being tested

Example Test using a Mock

Using Moq Syntax

Unconstrained Code Examples

Typemock Syntax

Code being tested

MS Fakes 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

The New Guy In Town

1

$ $ $

2

DateTime.Now

3

All that, plus:

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

My test

value

5

Mock

6

class DatabaseConnection

{

}

7

What can

I expect?

8

private members

9

JITCompilationStarted

10

Unconstrained

Constrained

11

public types & members

12

dynamic

13

Tests

14

More Info @

wrightfully.com/mocktalk

15

methods, properties, events

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

class DatabaseConnection

{

}

18

substitute

19

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

20

shim

21

class AnotherOne

{

}

22

static types & members

23

Using A Mocking Framework

24

File I/O

25

$$$

UI

$$

Integration

$

Unit Tests

26

FREE

27

158 members!

28

partial

29

Unconstrained

30

.VerifyAll()

31

Great for testing "legacy" code.

Best fit for code written with testing in mind.

32

interfaces

33

fake

34

virtual / abstract members

35

strict

36

Constrained