Shims 1.2.0-rc1
See the version list below for details.
dotnet add package Shims --version 1.2.0-rc1
NuGet\Install-Package Shims -Version 1.2.0-rc1
<PackageReference Include="Shims" Version="1.2.0-rc1" />
paket add Shims --version 1.2.0-rc1
#r "nuget: Shims, 1.2.0-rc1"
// Install Shims as a Cake Addin #addin nuget:?package=Shims&version=1.2.0-rc1&prerelease // Install Shims as a Cake Tool #tool nuget:?package=Shims&version=1.2.0-rc1&prerelease
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
Open your project in your preferred .NET IDE.
In the Package Manager Console, run the following command:
Install-Package Shims
Cloning the Repository
Clone the repository:
git clone https://github.com/Nixsoft-FR/Shims.git
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 | Versions 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. |
-
.NETFramework 4.8
- Lib.Harmony (>= 2.3.3)
-
.NETFramework 4.8.1
- Lib.Harmony (>= 2.3.3)
-
.NETStandard 2.0
- Lib.Harmony (>= 2.3.3)
-
net6.0
- Lib.Harmony (>= 2.3.3)
-
net7.0
- Lib.Harmony (>= 2.3.3)
-
net8.0
- Lib.Harmony (>= 2.3.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.