Using the AutoMoqCustomization i would have hoped my test would work but it fails.
this is the test:
[Test, AutoMoqData]
public void Test1(
[Frozen] MyObject myObject,
[Frozen] Mock<IRepo> stubMock,
MyClass sut,
int objectId)
{
myObject.Id = objectId;
MyObject result = sut.GetById(objectId);
Assert.That(result.Id, Is.EqualTo(myObject.Id));
}
i can get this to work if i add one line of code. But i would like not to have to do this because it should be inferred??
stubMock.Setup(r => r.GetObject(It.IsAny<int>())).Returns(() => myObject);
The MyClass has a constructor with the IRepo. This is working like a charm because if i use the line above in my test i have a passed test.
usually i write my test without the auto data it a lot more verbose:
[Test]
public void Test3()
{
IFixture fixture = new Fixture();
int objectId = fixture.Create<int>();
var stubMock = fixture.Freeze<Mock<IRepo>>();
stubMock.Setup(r => r.GetObject(It.IsAny<int>())).Returns(() => fixture.Create<MyObject>());
fixture.Freeze<MyObject>(customise => customise.With(d => d.Id, objectId));
var sut = new MyClass(stubMock.Object);
MyObject result = sut.GetById(objectId);
Assert.That(result.Id, Is.EqualTo(objectId));
}
So already my code is much cleaner but idd like that last cherry on top :) Any Idea's???
How to run:
Add Nuget packages:
<packages>
<package id="AutoFixture" version="3.19.2" targetFramework="net45" />
<package id="AutoFixture.AutoMoq" version="3.19.2" targetFramework="net45" />
<package id="AutoFixture.NUnit2" version="3.19.2" targetFramework="net45" />
<package id="Moq" version="3.1.416.3" targetFramework="net45" />
<package id="NUnit" version="2.6.3" targetFramework="net45" />
</packages>
Full Code:
public class AutoMoqDataAttribute : AutoDataAttribute
{
public AutoMoqDataAttribute()
: base(new Fixture().Customize(new AutoMoqCustomization()))
{
}
}
[TestFixture]
public class Class1
{
public interface IRepo
{
MyObject GetObject(int id);
}
public class MyObject
{
public int Id { get; set; }
}
public class MyClass
{
private readonly IRepo _test;
public MyClass(IRepo test) { _test = test; }
public MyObject GetById(int id) { return _test.GetObject(id); }
}
[Test, AutoMoqData]
public void Test1(
[Frozen] MyObject myObject, [Frozen] IRepo stubMock, MyClass sut, int objectId)
{
myObject.Id = objectId;
// expecting this commented line to be automatic because of the Frozen attribute on myObject?
// Mock.Get(stubMock).Setup(r => r.GetObject(It.IsAny<int>())).Returns(() => myObject);
MyObject result = sut.GetById(objectId);
Assert.That(result.Id, Is.EqualTo(myObject.Id));
}
[Test, AutoMoqData]
public void Test2(
[Frozen] MyObject myObject, [Frozen] Mock<IRepo> stubMock, MyClass sut, int objectId)
{
myObject.Id = objectId;
// expecting this commented line to be automatic because of the Frozen attribute on myObject?
// stubMock.Setup(r => r.GetObject(It.IsAny<int>())).Returns(() => myObject);
MyObject result = sut.GetById(objectId);
Assert.That(result.Id, Is.EqualTo(myObject.Id));
}
[Test]
public void Test3()
{
IFixture fixture = new Fixture();
int objectId = fixture.Create<int>();
var stubMock = fixture.Freeze<Mock<IRepo>>();
stubMock.Setup(r => r.GetObject(It.IsAny<int>())).Returns(() => fixture.Create<MyObject>());
fixture.Freeze<MyObject>(customise => customise.With(d => d.Id, objectId));
var sut = new MyClass(stubMock.Object);
MyObject result = sut.GetById(objectId);
Assert.That(result.Id, Is.EqualTo(objectId));
}
}
As of 3.20.0, you can use AutoConfiguredMoqCustomization
. This will automatically configure all mocks so that their members' return values are generated by AutoFixture.
Virtual methods/properties/indexers will be setup to lazily generate their return values using AutoFixture. If the method has out
parameters, those will be setup as well.
Public fields and sealed properties will have their values set eagerly.
This test should pass now:
[Test, AutoMoqData]
public void Test1(
[Frozen] MyObject myObject,
MyClass sut,
int objectId)
{
myObject.Id = objectId;
MyObject result = sut.GetById(objectId);
Assert.That(result.Id, Is.EqualTo(myObject.Id));
}