Using this structure.
public class User
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public virtual bool IsAdministrator()
{
return false;
}
}
Is it possible to combine AutoFixture and Moq to accomplish the following?
- Ensure
User.FirstNameis auto-generated - Ensure
User.LastNameis always "Smith" (literal) - Ensure
User.MiddleNameis not populated (default) - Ensure User.IsAdministrator() returns
True - Verify
IsAdministrator()was called.
I know this seems so simple. Here's what I tried using AutoMoq.
var config = new AutoMoqCustomization()
{
ConfigureMembers = true
};
var fixture = new AutoFixture.Fixture();
fixture.Customize(config);
fixture.Freeze<Mock<User>>()
.Setup(x => x.IsAdministrator())
.Returns(true);
var model = fixture.Build<User>()
.With(x => x.LastName, "Smith")
.Without(x => x.MiddleName)
.Create();
But that is clearly wrong. :( I am sure the syntax is simple. Thank you for any help.
My goal: create an object with BOTH filled properties and mocked methods.
The following achieves the requested goals
An observation was that when using
if would still populate the
MiddleNameproperty even though it was explicitly toldWithoutfor the build, but worked as desired when usingBut if you were to explicitly set the member to return
nullIt worked.
Here is another variation of the test with a subject class that depends on the model to be injected.