I started to use moq but from my understanding I always have to mock up all the methods that could be called even if I really do not care about them.
Sometimes it takes so long to mockup stuff you forget what you want to do. So I been looking at auto mocking but I am not sure what one I should use.
AutoFixture as an auto-mocking container
Automocking
I don't get how to use the first one at all. I sort of get the second one but never really tried it.
I am not sure if one is better than the other. The only thing I know is I am using AutoFixtures already what is a dependency of the first one.
So maybe in the long run it makes sense to go with the first one but like I said I can't find any basic tutorials on how to use it.
Edit
I am trying to follow "Nikos Baxevanis" examples but I am running into errors.
Failure: System.ArgumentException : A matching constructor for the given arguments was not found on the mocked type.
----> System.MissingMethodException : Constructor on type 'DatabaseProxyded46c36c8524889972231ef23659a72' not found.
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var fooMock = fixture.Freeze<Mock<IFoo>>();
// fooMock.Setup(x => x.GetAccounts(It.IsAny<IUnitOfWork>()));
var sut = fixture.CreateAnonymous<AdminService>();
sut.Apply();
fooMock.VerifyAll();
I think it is because of my petapoco unitOfWork property
PetaPoco.Database Db { get; }
Not sure if I got to mock this up somehow or what.
While I have never used moq-contrib Automocking
, I could probably provide some information on using AutoFixture as an auto-mocking container.
Currently there is support for Moq, Rhino Mocks, FakeItEasy, and NSubstitute. Just install the appropriate extension AutoMoq, AutoRhinoMocks, AutoFakeItEasy, and AutoNSubstitute.
Once you have installed one of the extensions for Auto Mocking the extra call is:
var fixture = new Fixture()
.Customize(new AutoMoqCustomization());
(or if you are using Rhino Mocks)
var fixture = new Fixture()
.Customize(new AutoRhinoMockCustomization());
(or if you are using FakeItEasy)
var fixture = new Fixture()
.Customize(new AutoFakeItEasyCustomization());
(or if you are using NSubstitute)
var fixture = new Fixture()
.Customize(new AutoNSubstituteCustomization());
Example 1
public class MyController : IController
{
public MyController(IFoo foo)
{
}
}
public interface IFoo
{
}
Here is how to use AutoFixture to create instances of MyController
class:
var fixture = new Fixture()
.Customize(new AutoMoqCustomization());
var sut = fixture.CreateAnonymous<MyController>();
Now, if you inspect the sut
variable you will see the the IFoo
is a mocked instance (having a type name similar to Castle.Proxies.IFooProxy).
Example 2
This examples extends the previous one.
You can instruct AutoFixture to use your own, pre-configured, mocked instance:
var fooMock = fixture.Freeze<Mock<IFoo>>();
// At this point you may setup expectation(s) on the fooMock.
var sut = fixture.CreateAnonymous<MyController>();
// This instance now uses the already created fooMock.
// Verify any expectation(s).
That's basically it - but it can go further!
Below are the previous examples using AutoFixture decleratively with the xUnit.net extension.
Example 1
[Theory, AutoMoqData]
public void TestMethod(MyController sut)
{
// Start using the sut instance directly.
}
Example 2
[Theory, AutoMoqData]
public void TestMethod([Frozen]Mock<IFoo> fooMock, MyController sut)
{
// At this point you may setup expectation(s) on the fooMock.
// The sut instance now uses the already created fooMock.
// Verify any expectation(s).
}
You may find more information on this blog post which contains links to everything related around AutoFixture, xUnit.net, and Auto Mocking.
Hope that helps.