Mocking and Automocking with Ben Amor

Following on from Amy's talk on Inversion of Control, Ben is going to delve into Mocking and AutoMocking, exploring how these techniques can help you write robust unit tests.

Join or login to comment.

  • Ben A.

    And here's an example of using it:

    [TestFixture]
    public class DailyDealService_Tests_with_luxury_base_class : TestBase<DailyDealService>
    { [Test]
    public void DailyDealService_returns_NotFound_if_the_repository_does_not_return_the_daily_deal()
    { // Arrange
    GetMock<IRepository<DailyDeal>>()
    .Setup(x => x.Read(It.IsAny<int>()))
    .Returns<DailyDeal>(null);

    // Act
    var result = ClassUnderTest.Purchase(It.IsAny<int>());

    // Assert
    Assert.That(result, Is.EqualTo(DailyDealPurchaseResult.NotFound));
    } }

    You'll need the nuget packages for Moq, AutoFixture, and "AutoFixture with Auto Mocking using Moq". Oh, and a unit testing framework :)

    If anyone would like the full code samples, flick me an email and I'll send you a zip.

    October 24, 2013

    • Richard R.

      Ben, if you still have the code for this can you send me it please, a bit late I know but only just got around to looking into this more! Email is [masked]. Thanks

      April 15, 2014

  • Ben A.

    Sure am :)

    Here's the base class:

    public class TestBase<T> where T : class
    { protected IFixture Fixture;

    [SetUp]
    public void Initialize()
    { Fixture = new Fixture().Customize(new AutoMoqCustomization());
    Fixture.Customize<T>(c => c.FromFactory(new MethodInvoker(new GreedyConstructorQuery())));
    }

    protected T ClassUnderTest
    { get
    { return Fixture.Freeze<T>();
    } }

    protected Mock<TDependency> GetMock<TDependency>() where TDependency : class
    { return Fixture.Freeze<Mock<TDependency>>();
    } }

    October 24, 2013

  • Steven R.

    Thanks Ben, a great talk. I've just written an email to the team I'm in, saying that we should look into using AutoFixture.

    Are you willing to post the base method that you showed us, and the example of calling it? If not, I'll email you.

    October 24, 2013

  • Ben A.

    Hey all, thanks again for letting me rattle on last night. The Q&A was particularly good, I thought. I get heaps out of chatting to folk about what they're up to.

    Here are some links.

    Frameworks
    - Moq (https://code.google.com/p/moq/­)
    - RhinoMocks (http://www.nuget.org/packages/r...­)
    - NMock (http://nmock3.codeplex.com/­)
    - NSubstitute (http://nsubstitute.github.io/)&;...;­

    AutoFixture
    - http://autofixture.codeplex.com...­
    - http://blog.ploeh.dk/2010/08/19...­

    StructureMap automocker
    - http://docs.structuremap.net/AutoMocker.htm

    Martin Fowler's article on the various sorts of test double
    - http://martinfowler.com/articles/mocksArentStubs.html


    If anyone's got any questions about mocking or anything else related, hit me up. I love chatting about this sort of thing.

    1 · October 24, 2013

35 went

Our Sponsors

  • Make a Donation

    You can help cover group admin, venue and presenter costs by donating.

  • MS Communities

    The nationwide coordination for Microsoft user groups

  • Microsoft

    Major sponsor for MS Communites (the user group association).

  • Inov8

    Hosting our user group meetings

  • Mindscape
  • Xero

    Provided a venue for many years...

People in this
Meetup are also in:

Sometimes the best Meetup Group is the one you start

Get started Learn more
Katie

I'm surprised by the level of growth I've seen since becoming an organizer, it's given me more confidence in my abilities.

Katie, started NYC ICO

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy