Skip to content
/ pose Public
forked from tonerdo/pose

A lightweight unconstrained isolation framework for .NET Standard

License

Notifications You must be signed in to change notification settings

gps035/pose

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Windows build status License: MIT NuGet version

Pose

Pose allows you to replace any .NET method (including static and non-virtual) with a delegate. It is similar to Microsoft Fakes but unlike it Pose is implemented entirely in managed code (Reflection Emit API). Everything occurs at runtime and in-memory, no unmanaged Profiling APIs and no file system pollution with re-written assemblies.

Pose is cross platform and runs anywhere .NET is supported. It targets .NET Standard 2.0 so it can be used across .NET platforms including .NET Framework, .NET Core, Mono and Xamarin.

Installation

Available on NuGet

Visual Studio:

PM> Install-Package Pose

.NET Core CLI:

dotnet add package Pose

Usage

Pose gives you the ability to create shims by way of the Shim class. Shims are basically objects that let you specify the method you want to replace as well as the replacement delegate. Delegate signatures (arguments and return type) must match that of the methods they replace. The Is class is used to create instances of a type and all code you want to apply your shims to is isolated using the PoseContext class.

Shim static method

using Pose;

Shim consoleShim = Shim.Replace(() => Console.WriteLine(Is.A<string>())).With(
    delegate (string s) { Console.WriteLine("Hijacked: {0}", s); });

Shim static property getter

using Pose;

Shim dateTimeShim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(2004, 4, 4));

Shim instance property getter

using Pose;

class MyClass
{
    public int MyProperty { get; set; }
    public void DoSomething() => Console.WriteLine("doing someting");
}

Shim classPropShim = Shim.Replace(() => Is.A<MyClass>().MyProperty).With((MyClass @this) => 100);

Shim instance method of a Reference Type

using Pose;

Shim classShim = Shim.Replace(() => Is.A<MyClass>().DoSomething()).With(
    delegate (MyClass @this) { Console.WriteLine("doing someting else"); });

Note: The first argument to an instance method replacement delegate is always the instance of the class

Shim method of specific instance of a Reference Type

using Pose;

MyClass myClass = new MyClass();
Shim myClassShim = Shim.Replace(() => myClass.DoSomething()).With(
    delegate (MyClass @this) { Console.WriteLine("doing someting else with myClass"); });

Shim instance method of a Value Type

using Pose;

Shim structShim = Shim.Replace(() => Is.A<MyStruct>().DoSomething()).With(
    delegate (ref MyStruct @this) { Console.WriteLine("doing someting else"); });

Note: You cannot shim methods on specific instances of Value Types

Isolating your code

// This block executes immediately
PoseContext.Isolate(() =>
{
    // All code that executes within this block
    // is isolated and shimmed methods are replaced

    // Outputs "Hijacked: Hello World!"
    Console.WriteLine("Hello World!");

    // Outputs "4/4/04 12:00:00 AM"
    Console.WriteLine(DateTime.Now);

    // Outputs "doing someting else"
    new MyClass().DoSomething();

    // Outputs "doing someting else with myClass"
    myClass.DoSomething();

}, consoleShim, dateTimeShim, classPropShim, classShim, myClassShim, structShim);

Roadmap

  • Constructor Shimming - Ability to create shims for constructors.
  • Performance Improvements - Pose can be used outside the context of unit tests. Better performance would make it suitable for use in production code, possibly to override legacy functionality.
  • Exceptions Stack Trace - Currently when exceptions are thrown in your own code under isolation, the supplied exception stack trace is quite confusing. Providing an undiluted exception stack trace is needed.

Issues & Contributions

If you find a bug or have a feature request, please report them at this repository's issues section. Contributions are highly welcome, however, except for very small changes kindly file an issue and let's have a discussion before you open a pull request.

License

This project is licensed under the MIT license. See the LICENSE file for more info.

About

A lightweight unconstrained isolation framework for .NET Standard

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%