Stashbox.Moq
4.5.1
See the version list below for details.
dotnet add package Stashbox.Moq --version 4.5.1
NuGet\Install-Package Stashbox.Moq -Version 4.5.1
<PackageReference Include="Stashbox.Moq" Version="4.5.1" />
paket add Stashbox.Moq --version 4.5.1
#r "nuget: Stashbox.Moq, 4.5.1"
// Install Stashbox.Moq as a Cake Addin #addin nuget:?package=Stashbox.Moq&version=4.5.1 // Install Stashbox.Moq as a Cake Tool #tool nuget:?package=Stashbox.Moq&version=4.5.1
stashbox-mocking
Mocking framework integrations for Stashbox that provide automatic mock creation for your services in unit tests.
Moq | FakeItEasy | NSubstitute | RhinoMocks |
---|---|---|---|
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 ofStashMoq
with that it will call theVerifyAll()
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 Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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 | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
.NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
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.8.0 | 218 | 10/1/2024 |
4.7.1 | 143 | 4/8/2024 |
4.7.0 | 5,812 | 6/21/2023 |
4.6.0 | 158 | 6/5/2023 |
4.5.2 | 409 | 3/29/2023 |
4.5.1 | 219 | 3/29/2023 |
4.5.0 | 252 | 2/28/2023 |
4.4.1 | 403 | 1/20/2023 |
4.4.0 | 866 | 12/6/2022 |
4.3.2 | 338 | 11/29/2022 |
4.3.1 | 435 | 10/14/2022 |
4.3.0 | 406 | 10/12/2022 |
4.2.3 | 440 | 9/9/2022 |
4.2.2 | 463 | 6/2/2022 |
4.2.1 | 4,065 | 5/17/2022 |
4.2.0 | 5,677 | 5/3/2022 |
4.1.2 | 593 | 3/12/2022 |
4.1.1 | 468 | 3/12/2022 |
4.1.0 | 464 | 3/7/2022 |
4.0.1 | 677 | 2/10/2022 |
4.0.0 | 684 | 2/9/2022 |
3.0.0 | 564 | 11/22/2021 |
2.4.3 | 1,236 | 5/26/2021 |
2.4.2 | 695 | 3/16/2021 |
2.4.1 | 571 | 11/15/2020 |
2.4.0 | 488 | 11/3/2020 |
2.3.6 | 471 | 11/2/2020 |
2.3.5 | 540 | 10/19/2020 |
2.3.4 | 536 | 10/19/2020 |
2.3.3 | 519 | 10/19/2020 |
2.3.2 | 22,443 | 6/30/2020 |
2.3.1 | 679 | 6/22/2020 |
2.3.0 | 612 | 6/8/2020 |
2.2.1 | 538 | 6/8/2020 |
2.1.5 | 6,163 | 11/11/2019 |
2.1.4 | 668 | 10/4/2019 |
2.1.2 | 1,149 | 9/12/2019 |
2.1.1 | 646 | 9/11/2019 |
1.4.7 | 1,815 | 6/10/2019 |
1.4.6 | 13,247 | 1/13/2019 |
1.4.5 | 812 | 12/27/2018 |
1.4.3 | 1,029 | 10/20/2018 |
1.4.2 | 16,631 | 6/15/2018 |
1.4.1 | 958 | 6/15/2018 |
1.3.9 | 6,811 | 3/20/2018 |
1.3.8 | 1,841 | 2/8/2018 |
1.3.6 | 1,111 | 2/5/2018 |
1.2.11 | 9,195 | 11/24/2017 |
1.2.8 | 1,017 | 8/7/2017 |
1.2.7 | 978 | 6/28/2017 |
1.2.6 | 1,137 | 6/9/2017 |
1.2.4 | 1,073 | 5/18/2017 |
1.2.3 | 1,081 | 5/16/2017 |
1.2.2 | 1,078 | 5/16/2017 |
1.2.1 | 1,061 | 5/15/2017 |