-
-
Save darrencauthon/1299009 to your computer and use it in GitHub Desktop.
AutoMoq problem when calling twice in a very specific scenario
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.