Stashbox.NSubstitute 4.2.1

.NET Standard 2.1 .NET Framework 4.5
Install-Package Stashbox.NSubstitute -Version 4.2.1
dotnet add package Stashbox.NSubstitute --version 4.2.1
<PackageReference Include="Stashbox.NSubstitute" Version="4.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Stashbox.NSubstitute --version 4.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Stashbox.NSubstitute, 4.2.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Stashbox.NSubstitute as a Cake Addin
#addin nuget:?package=Stashbox.NSubstitute&version=4.2.1

// Install Stashbox.NSubstitute as a Cake Tool
#tool nuget:?package=Stashbox.NSubstitute&version=4.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

stashbox-mocking

Appveyor build status GitHub Workflow Status Sourcelink

Mocking framework integrations for Stashbox that provide automatic mock creation for your services in unit tests.

Moq FakeItEasy NSubstitute RhinoMocks
NuGet Version NuGet Version NuGet Version NuGet Version

Moq

You can use the auto mock framework by creating a StashMoq instance wrapped in a using statement, on its disposal it will call Verify() on all the configured expectations.

//begin a test scope
using (var stash = StashMoq.Create())
{
    //configure a mock dependency
    stash.Mock<IDependency>().Setup(m => m.Test()).Returns("test");
    
    //configure the mock again
    //this call will get the same mock back as the first request
    stash.Mock<IDependency>().Setup(m => m.Test2());
    
    //get the tested service filled with auto created mocks (except the configured ones)
    var service = stash.Get<IService>();
    
    //call the tested method, imagine that this will invoke the Test() method of an IDependency
    var result = service.Test();
    
    //check the result
    Assert.Equal("test", result);
    
} //StashMoq will call the Verify() method on all configured expectations on its dispose

You can also set the verifyAll parameter of StashMoq with that it will call the VerifyAll() on the used mock repository. StashMoq.Create(verifyAll: true)

Mock behavior

You can set which mock behavior should be used by the framework by default.

using (var stash = StashMoq.Create(MockBehavior.Strict)) //the default will be strict
{
    //this mock will be strict
    stash.Mock<IDependency>().Setup(m => m.Test()).Returns("test");
    
    //you can also override the default config, this mock will be loose
    stash.Mock<IDependency2>(MockBehavior.Loose).Setup(...);
}

FakeItEasy

You can use the auto mock framework by creating a StashItEasy instance wrapped in a using statement.

//begin a test scope
using (var stash = StashItEasy.Create())
{
    //configure a mock dependency
    var fake = stash.Fake<IDependency>();
    
    //configure the call
    A.CallTo(() => fake.Test()).Returns("test");
    
    //get the tested service filled with auto created fakes (except the configured ones)
    var service = stash.Get<IService>();
    
    //call the tested method, imagine that this will invoke the Test() method of the IDependency
    var result = service.Test();
    
    //check the call
    A.CallTo(() => fake.Test()).MustHaveHappened();
    
    //check the result
    Assert.Equal("test", result);    
}

Options

You can set what fake options should be used by the framework by default.

using (var stash = StashItEasy.Create(x => x.Strict())) //the default will be strict
{
    //this fake will be strict
    stash.Fake<IDependency>();
    
    //you can also override the default config
    stash.Fake<IDependency>(x => x.Implements<IDependency3>());
}

NSubstitute

You can use the auto mock framework by creating a StashSubstitute instance wrapped in a using statement.

//begin a test scope
using (var stash = StashSubstitute.Create())
{
    //configure a mock dependency
    var sub = stash.Sub<IDependency>(); //for multiple interface implementations use the overloads of this method
    sub.Test().Returns("test");
    
    //get the tested service filled with auto created mocks (except the configured ones)
    var service = stash.Get<IService>();
    
    //call the tested method, imagine that this will invoke the Test() method of an IDependency
    var result = service.Test();
    
    //check the call
    sub.Recieved().Test();
    
    //check the result
    Assert.Equal("test", result);   
}

You can also get a partial mock with the stash.Partial<IDependency>() call.

RhinoMocks

You can use the auto mock framework by creating a StashRhino instance wrapped in a using statement, on its disposal it will call VerifyAllExpectations() on all the configured expectations.

//begin a test scope
using (var stash = StashRhino.Create())
{
    //configure a mock dependency
    stash.Mock<IDependency>().Expect(x => x.Test()).Returns("test");
    
    //configure the mock again
    //this call will get the same mock back as the first request
    stash.Mock<IDependency>().Expect(m => m.Test2());
    
    //get the tested service filled with auto created mocks (except the configured ones)
    var service = stash.Get<IService>();
    
    //call the tested method, imagine that this will invoke the Test() method of an IDependency
    var result = service.Test();
    
    //check the result
    Assert.Equal("test", result);   
    
} //StashRhino will call the VerifyAllExpectations() method on all configured expectations on its dispose

Mock types

You can also request different mock types from StashRhino:

using (var stash = StashRhino.Create())
{
    //this will create a dynamic mock
    stash.Mock<IDependency>();
    
    //this will create a strict mock
    stash.Strict<IDependency>();
    
    //this will create a partial mock
    stash.Partial<IDependency>();
}

Further things that each package offers

  • All package allows the service instantiation by a selected constructor with pre-evaluated arguments:
var service = stash.GetWithConstructorArgs<Service>(mockObject1, mockObject2);

//you can also use a placeholder argument where you don't want to set a concrete object
var service = stash.GetWithConstructorArgs<Service>(StashArg.Any<IMock>(), mockObject2);

If you use an argument placeholder with a non-mockable type, the framework will throw a NonMockableTypeException.

  • All package allows the dependency override with pre-evaluated dependencies:
//this will inject the `mockObject1` into the created `Service` everywhere it fits by its type
var service = stash.GetWithParamOverrides<Service>(mockObject1);
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.1
.NET Framework net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
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
4.2.1 46 5/17/2022
4.2.0 44 5/3/2022
4.1.2 104 3/12/2022
4.1.1 65 3/12/2022
4.1.0 60 3/7/2022
4.0.1 61 2/10/2022
4.0.0 50 2/9/2022
3.0.0 263 11/22/2021
2.4.3 193 5/26/2021
2.4.2 140 3/16/2021
2.4.1 320 11/15/2020
2.4.0 227 11/3/2020
2.3.6 195 11/2/2020
2.3.5 266 10/19/2020
2.3.4 268 10/19/2020
2.3.3 290 10/19/2020
2.3.2 482 6/30/2020
2.3.1 286 6/22/2020
2.3.0 264 6/8/2020
2.2.1 285 6/8/2020
2.1.5 865 11/11/2019
2.1.4 391 10/4/2019
2.1.2 425 9/12/2019
2.1.1 311 9/11/2019
1.4.7 677 6/10/2019
1.4.6 1,230 1/13/2019
1.4.5 602 12/27/2018
1.4.3 552 10/20/2018
1.4.2 642 6/15/2018
1.4.1 634 6/15/2018
1.3.9 678 3/20/2018
1.3.8 661 2/8/2018
1.3.6 715 2/5/2018
1.2.11 606 11/24/2017
1.2.8 753 8/7/2017
1.2.7 738 6/28/2017
1.2.6 742 6/9/2017
1.2.4 722 5/18/2017
1.2.3 760 5/16/2017
1.2.2 750 5/16/2017
1.2.1 717 5/15/2017