Poser allows you to replace any .NET method (including static and non-virtual) with a delegate. It is similar to Microsoft Fakes but unlike it Poser 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.
Poser 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. See version compatibility table here.
Available on NuGet
Visual Studio:
PM>Install-Package Poser.NET Core CLI:
dotnet add package PoserPoser 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.
usingPose;ShimconsoleShim=Shim.Replace(()=>Console.WriteLine(Is.A<string>())).With(delegate(strings){Console.WriteLine("Hijacked: {0}",s);});usingPose;ShimdateTimeShim=Shim.Replace(()=>DateTime.Now).With(()=>newDateTime(2004,4,4));usingPose;ShimsetterShim=Shim.Replace(()=>Console.Title,true).With((stringtitle)=>{Console.Title="My Title";});usingPose;classMyClass{publicintMyProperty{get;set;}publicvoidDoSomething()=>Console.WriteLine("doing someting");}ShimclassPropShim=Shim.Replace(()=>Is.A<MyClass>().MyProperty).With((MyClass@this)=>100);usingPose;ShimclassPropShim=Shim.Replace(()=>Is.A<MyClass>().MyProperty,true).With((MyClass@this,intprop)=>{@this.MyProperty=prop*10;});usingPose;ShimctorShim=Shim.Replace(()=>newMyClass()).With(()=>newMyClass(){MyProperty=10});usingPose;ShimclassShim=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
usingPose;MyClassmyClass=newMyClass();ShimmyClassShim=Shim.Replace(()=>myClass.DoSomething()).With(delegate(MyClass@this){Console.WriteLine("doing someting else with myClass");});usingPose;ShimstructShim=Shim.Replace(()=>Is.A<MyStruct>().DoSomething()).With(delegate(refMyStruct@this){Console.WriteLine("doing someting else");});Note: You cannot shim methods on specific instances of Value Types
varoperatorShim=Shim.Replace(()=>Is.A<TimeSpan>()+Is.A<TimeSpan>()).With(delegate(TimeSpanl,TimeSpanr){returnTimeSpan.Zero;});// This block executes immediatelyPoseContext.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"newMyClass().DoSomething();// Outputs "doing someting else with myClass"myClass.DoSomething();// Outputs '00:00:00'Console.WriteLine(TimeSpan.FromDays(1)+TimeSpan.FromSeconds(2));},consoleShim,dateTimeShim,classPropShim,classShim,myClassShim,structShim,operatorShim);Operator shimming requires that the class/struct overloads the operator in question.
Poser supports shimming operators of the following kind:
- Arithmetic
+x-x!x~xx + yx - yx / yx % yx & yx | yx ^ yx << yx >> y
- Equality
x == yx != y
- Comparison
x < yx > yx <= yx >= y
In addition to this, both implicit and explicit conversion operators are supported.
Shimming of the following operators is not supported:
trueandfalsebecause I cannot find a good way to express the operation in an expression tree.x >>> ybecause expression trees cannot contain this operator. This is a limitation on the part of the compiler.++and--because these cannot be expressed in an expression tree.
Breakpoints - At this time any breakpoints set anywhere in the isolated code and its execution path will not be hit. However, breakpoints set within a shim replacement delegate are hit.
Exceptions - At this time all unhandled exceptions thrown in isolated code and its execution path are always wrapped in
System.Reflection.TargetInvocationException.Mocks & Asserts - Mock setups and asserts must not be done within the isolated code. This is because the isolated code is executed in a different context and not the test context. Following is the correct way to include mock setups and asserts when using Poser:
varosShim=Shim.Replace(()=>OperatingSystem.IsWindows()).With(()=>false);varresult=true;PoseContext.Isolate(()=>{// Store the resultresult=OperatingSystem.IsWindows();},osShim);// Assert the result outside the isolated contextAssert.False(result);
- Performance Improvements - Poser 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.
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.
This project is licensed under the MIT license. See the LICENSE file for more info.