All Projects → tonerdo → Pose

tonerdo / Pose

Licence: mit
Replace any .NET method (including static and non-virtual) with a delegate

Programming Languages

csharp
926 projects

Projects that are alternatives of or similar to Pose

Nsubstitute
A friendly substitute for .NET mocking libraries.
Stars: ✭ 1,646 (+92.51%)
Mutual labels:  dotnet-core, mocking
Fakeiteasy
The easy mocking library for .NET
Stars: ✭ 1,092 (+27.72%)
Mutual labels:  dotnet-core, mocking
Automation Arsenal
Curated list of popular Java and Kotlin frameworks, libraries and tools related to software testing, quality assurance and adjacent processes automation.
Stars: ✭ 105 (-87.72%)
Mutual labels:  mocking, testing-framework
Mockqueryable
Moking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc
Stars: ✭ 281 (-67.13%)
Mutual labels:  dotnet-core, mocking
Builderhmi.lite
.NET Core WPF UI design as quick and intuitive as WinForms! Did you hear that MICROSOFT??
Stars: ✭ 25 (-97.08%)
Mutual labels:  dotnet-core
Aruba
Test command-line applications with Cucumber-Ruby, RSpec or Minitest. The most up to date documentation can be found on Cucumber.Pro (https://app.cucumber.pro/projects/aruba)
Stars: ✭ 900 (+5.26%)
Mutual labels:  testing-framework
Grpc
A proof of concept for a way to implement gRPC services in a code first way using C# and .NET Core.
Stars: ✭ 17 (-98.01%)
Mutual labels:  dotnet-core
Spotifyapi Net
🔉 A Client for the Spotify Web API, written in C#/.NET
Stars: ✭ 887 (+3.74%)
Mutual labels:  dotnet-core
Weixinmpsdk
微信全平台 SDK Senparc.Weixin for C#,支持 .NET Framework 及 .NET Core、.NET 6.0。已支持微信公众号、小程序、小游戏、企业号、企业微信、开放平台、微信支付、JSSDK、微信周边等全平台。 WeChat SDK for C#.
Stars: ✭ 7,098 (+730.18%)
Mutual labels:  dotnet-core
Lettuceencrypt
Free, automatic HTTPS certificate generation for ASP.NET Core web apps
Stars: ✭ 939 (+9.82%)
Mutual labels:  dotnet-core
Cqrslite
A lightweight framework to help creating CQRS and Eventsourcing applications in C#
Stars: ✭ 925 (+8.19%)
Mutual labels:  dotnet-core
Polygen
PolyGen is a code generator that produces database schema, ORM layer, REST API and a (coming soon — stay tuned!) single-page web UI for your business model.
Stars: ✭ 19 (-97.78%)
Mutual labels:  dotnet-core
Unilinks
Plataforma para encontrar os links das aulas virtuais gravadas.
Stars: ✭ 25 (-97.08%)
Mutual labels:  dotnet-core
Dotnet Ignore
.NET CLI tool that can download .gitignore file from gitignore repository
Stars: ✭ 18 (-97.89%)
Mutual labels:  dotnet-core
Clj Fakes
An isolation framework for Clojure/ClojureScript.
Stars: ✭ 26 (-96.96%)
Mutual labels:  mocking
Nadekobot
Open source, general-purpose Discord chat bot written in C#
Stars: ✭ 892 (+4.33%)
Mutual labels:  dotnet-core
Pytest Responsemock
Simplified requests calls mocking for pytest
Stars: ✭ 24 (-97.19%)
Mutual labels:  mocking
Awesome Dotnet Core
.NET Core库、工具、框架和软件的中文收录大全。 内容包括:库、工具、框架、模板引擎、身份认证、数据库、ORM框架、图片处理、文本处理、机器学习、日志、代码分析、教程等。
Stars: ✭ 929 (+8.65%)
Mutual labels:  dotnet-core
Cli
a lightweight, security focused, BDD test framework against terraform.
Stars: ✭ 918 (+7.37%)
Mutual labels:  testing-framework
Unitube
An open source client for YouTube.
Stars: ✭ 22 (-97.43%)
Mutual labels:  dotnet-core

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. See version compatibility table here.

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 static property setter

using Pose;

Shim setterShim = Shim.Replace(() => Console.Title, true).With((string title) => { Console.Title = "My Title"; });

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 property setter

using Pose;

Shim classPropShim = Shim.Replace(() => Is.A<MyClass>().MyProperty, true).With((MyClass @this, int prop) => { @this.MyProperty = prop * 10; });

Shim constructor

using Pose;

Shim ctorShim = Shim.Replace(() => new MyClass()).With(() => new MyClass() { MyProperty = 10 });

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

Caveats & Limitations

  • 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.

Roadmap

  • 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].