Shims 1.2.0

dotnet add package Shims --version 1.2.0                
NuGet\Install-Package Shims -Version 1.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Shims" Version="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Shims --version 1.2.0                
#r "nuget: Shims, 1.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Shims as a Cake Addin
#addin nuget:?package=Shims&version=1.2.0

// Install Shims as a Cake Tool
#tool nuget:?package=Shims&version=1.2.0                

Shims

Shims is a .NET project that allows the creation of unit tests for class methods and properties using shims (mocking).

Features

  • Creation of shims for methods
  • Unit tests for property getters
  • Handling exceptions and callbacks in tests

Installation

Via NuGet Package

  1. Open your project in your preferred .NET IDE.

  2. In the Package Manager Console, run the following command:

    Install-Package Shims
    

Cloning the Repository

  1. Clone the repository:

    git clone https://github.com/Nixsoft-FR/Shims.git
    
  2. Open the project in your preferred .NET IDE.

Usage

Prerequisites

Ensure your unit test classes inherit from UnitTestBase to fully leverage shim features:

public class MyTestClass : UnitTestBase
{
    // Your test methods here
}

Creating a Shim for a Method

To create a shim for a method in MyClass, you can use the Shim<MyClass> class:

Shim<MyClass> shim = new Shim<MyClass>();
shim.Setup(mock => mock.MyMethod()).Returns("Hello Shim!");

MyClass instance = new MyClass();
string result = instance.MyMethod();

Assert.AreEqual("Hello Shim!", result);

Testing a Property Getter

To test a property getter in MyClass, use Shim<MyClass> and SetupGet:

[TestMethod]
public void MyClass_MyPropertyString_Get()
{
    Shim<MyClass> shim = new Shim<MyClass>();
    shim.SetupGet(mock => mock.MyPropertyString).Returns("Test Value");
    MyClass instance = new MyClass();
    Assert.AreEqual("Test Value", instance.MyPropertyString);
}

Handling Exceptions and Callbacks

You can also handle exceptions and callbacks in your tests:

[TestMethod]
public void MyClass_MyMethod_ThrowsException()
{
    Shim<MyClass> shim = new Shim<MyClass>();
    shim.Setup(mock => mock.MyMethod()).Throws<ArgumentException>();

    MyClass instance = new MyClass();
    Assert.ThrowsException<ArgumentException>(() => instance.MyMethod());
}

[TestMethod]
public void MyClass_MyMethod_WithCallback()
{
    bool callbackCalled = false;

    Shim<MyClass> shim = new Shim<MyClass>();
    shim.Setup(mock => mock.MyMethod()).Callback(() => callbackCalled = true);

    MyClass instance = new MyClass();
    instance.MyMethod();

    Assert.IsTrue(callbackCalled);
}

Acknowledgments

Special thanks to the Harmony.Lib library for its use in this project.

We also want to thank the Moq library, which greatly inspired the syntax used in Shims.

Contribution

Contributions are welcome! Please submit an issue or a pull request for any suggestions or improvements.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 77 7/24/2024
1.2.0-rc1 58 7/24/2024
1.1.0 65 7/17/2024
1.0.1 77 6/25/2024
1.0.0 75 6/17/2024