Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Mocking .NET Without Hurting Its Feelings (CodeMash 2018)

No description
by

John Wright

on 9 January 2018

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Mocking .NET Without Hurting Its Feelings (CodeMash 2018)

What can they do?
Why use a Mocking Framework?
vs
John M. Wright
Mocking .NET Without Hurting Its Feelings
john@wrightfully.com
@wright2tweet
www.wrightfully.com
UI
Integration
Unit Tests
$$$
$$
$
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();
}
}

}
File I/O
class AnotherOne
{

}
DateTime.Now
mock types
class DatabaseConnection
{

}
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 DatabaseConnection
{

}
My test
value
strict
partial
dynamic
A rose by any other name
would smell as sweet
work about the same
stub
http://mimiandeunice.com/2011/07/19/expectations/
mock
fake
substitute
What can
I expect?
wrightfully.com/mocktalk
Constrained
Unconstrained
methods
properties
events
public
internal
static
sealed
private
FREE
All that, plus:
$ $ $
Per 5 Build Servers:
Constrained
Unconstrained
Open source
No extra software needed
Limited commercial support
May require design compromises
.NET libs
3rd Party libs (Sharepoint)
Proprietary (closed source)
Some require profiler application / VS plugin
Commercial support
Best fit for code written with testing in mind.
Great for testing "legacy" code.
Using Hand-Crafted, Artisanal Mocks
Class Being Tested
Tests
Custom Mocks
158 members!
Using A Mocking Framework
How do they do it?
shim
Using Moq Syntax
Code Examples (aka: What can you do?)
Unconstrained Code Examples
Code being tested
Typemock Syntax
MS Fakes Syntax
test double
mock
vs
stub
mock.VerifyAllExpectations();

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

CodeMash
Jan 12th, 2018
Constrained Frameworks:
Class Inheritance On-Demand

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
Unconstrained Frameworks:
Make Your Own Man-In-The-Middle Attack
ICorProfilerInfo::
SetILFunctionBody
JITCompilationStarted
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
Constrained Frameworks:
Limitations of Class Inheritance

virtual / abstract members
public types & members
internal types & members
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2,
PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
© Copyright 2018 John M. Wright
[assembly: InternalsVisibleTo("Your.TestAssembly")]
AssemblyInfo.cs
Mocking Internal Types/Members
private members
static types & members
sealed types
interfaces
methods, properties, events
More Info @
wrightfully.com/mocktalk
Code being tested
Example Test using a Mock
.VerifyAll()
Mock
The New Guy In Town
Full transcript