Using AutoMoq

This is an example of using AutoMoq.

It requires the use of NUnit and the AutoMoq NuGet packages.

As far as I can see there is no difference between the Create and Resolve methods.

ILSpy confirms that the generic resolve calls the generic create method.

using AutoMoq;
using Moq;
using NUnit.Framework;

namespace TestLib
{
public interface IFoo
{
string Execute();
}

public class Class1
{
private IFoo _foo;

public Class1(IFoo foo)
{
_foo = foo;
}

public string Write()
{
return string.Format(“>{0}<“, _foo.Execute());
}
}

[TestFixture]
public class TestMe
{
AutoMoqer _autoMoqer;

[SetUp]
public void Setup()
{
_autoMoqer = new AutoMoqer();
}

[Test]
public void A()
{
Mock<IFoo> mockFoo = _autoMoqer.GetMock<IFoo>();
mockFoo.Setup(a => a.Execute()).Returns(“Hello”);
Class1 c = _autoMoqer.Create<Class1>();

//Act and Assert
Assert.That(c.Write(), Is.EqualTo(“>Hello<“));
}

[Test]
public void B()
{
var c = _autoMoqer.Resolve<Class1>();

//Act and Assert
Assert.That(c.Write(), Is.EqualTo(“><“));
}
}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s