Skip to content

Instantly share code, notes, and snippets.

@darrencauthon
Forked from giggio/AutomoqTest.cs
Created October 19, 2011 17:25
Show Gist options
  • Save darrencauthon/1299009 to your computer and use it in GitHub Desktop.
Save darrencauthon/1299009 to your computer and use it in GitHub Desktop.
AutoMoq problem when calling twice in a very specific scenario
using AutoMoq;
using NUnit.Framework;
namespace TestProject1
{
[TestFixture]
public class UnitTest1
{
private AutoMoqer mocker;
private SomeController controller;
private const string Id = "the id";
[SetUp]
public void Setup()
{
mocker = new AutoMoqer();
var profile = new Profile();
controller = mocker.Create<SomeController>();
mocker.GetMock<IProfilerGetter>().Setup(p => p.Get(Id)).Returns(profile);
}
[Test]
public void CanAMockGeneratedByAutomoqBeCalledOnce()
{
var p1 = controller.Get(Id);
Assert.IsNotNull(p1);
}
[Test]
public void CanAMockGeneratedByAutomoqBeCalledTwice()
{
var p2 = controller.Get(Id);
Assert.IsNotNull(p2);
}
}
public class SomeController
{
private readonly IProfilerGetter profilerGetter;
public SomeController(IProfilerGetter profilerGetter)
{
this.profilerGetter = profilerGetter;
}
public Profile Get(string id)
{
return profilerGetter.Get(id);
}
}
public interface IProfilerGetter
{
Profile Get(string id);
}
public class Profile
{
}
}
@darrencauthon
Copy link
Author

Instead of creating one AutoMoqer instance that is shared for all of the tests, I new'd up a new instance for every setup.

I don't know why it was erroring, but I think the reason why I never came across this after using AutoMoq in thousands of tests is that I never let any state carry over from one test to another. I think new'ing up a different AutoMoq instance will save you more grief in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment