Preventing Autofixture from filling child collections - c#

I'm using the latest version of Autofixture, and I'd like to prevent it from filling automatically child collections.
For example, I have a class Person that has a List property. I want all properties filled, except the list.
I tried using this customization :
public class RemoveMultiples : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations
.OfType<FilteringSpecimenBuilder>()
.Where(x => x.Specification is DictionarySpecification)
.ToList().ForEach(c => fixture.Customizations.Remove(c));
fixture.Customizations
.OfType<FilteringSpecimenBuilder>()
.Where(x => x.Specification is CollectionSpecification)
.ToList().ForEach(c => fixture.Customizations.Remove(c));
fixture.Customizations
.OfType<FilteringSpecimenBuilder>()
.Where(x => x.Specification is HashSetSpecification)
.ToList().ForEach(c => fixture.Customizations.Remove(c));
fixture.Customizations
.OfType<FilteringSpecimenBuilder>()
.Where(x => x.Specification is ListSpecification)
.ToList().ForEach(c => fixture.Customizations.Remove(c));
}
}
But it also prevents me from using .CreateMany().
edit: I can use .CreateMany(3) and it works.
Is there a setting somewhere that could let me autofill collections only when I need to?
edit2: Class person should look like this:
[Serializable]
public class Person
{
private ICollection<OtherClass> _otherClasses;
private string _something;
public virtual ICollection<OtherClass> OtherClasses
{
get { return _otherClasses; }
set { _otherClasses = value; }
}
}
Note that it's not always a Collection, but sometimes IList
Note2: I just realized that someone also removed the Customization for IEnumerable hence why the CreateMany() doesn't create anything.

Here's one way to do it.
Start by implementing a SpecimenBuilder that tells AutoFixture to skip assigning a value for collection property:
public class CollectionPropertyOmitter : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
var pi = request as PropertyInfo;
if (pi != null
&& pi.PropertyType.IsGenericType
&& pi.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>))
return new OmitSpecimen();
return new NoSpecimen(request);
}
}
Then encapsulate that in a Customization:
public class DoNotFillCollectionProperties : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(new CollectionPropertyOmitter());
}
}
The following tests now pass:
[Fact]
public void CreatePersonWithoutFillingCollectionProperty()
{
var fixture = new Fixture().Customize(new DoNotFillCollectionProperties());
var actual = fixture.Create<Person>();
Assert.Null(actual.OtherClasses);
}
[Fact]
public void CreateManyStillWorks()
{
var fixture = new Fixture().Customize(new DoNotFillCollectionProperties());
var actual = fixture.CreateMany<Person>();
Assert.NotEmpty(actual);
}
[Fact]
public void CreatListStillWorks()
{
var fixture = new Fixture().Customize(new DoNotFillCollectionProperties());
var actual = fixture.Create<List<Person>>();
Assert.NotEmpty(actual);
}
[Fact]
public void CreateCollectionStillWorks()
{
var fixture = new Fixture().Customize(new DoNotFillCollectionProperties());
var actual = fixture.Create<ICollection<Person>>();
Assert.NotEmpty(actual);
}

Related

Moq Setup dont working, while trying to automate it

Main Goal : Automatic Moq an entire DBContext
Current Problem: Setup donĀ“t work. It runs and adds the Setup but when you try to access the Data you get an "NotImplemented Exception"
The member 'IQueryable.Provider' has not been implemented on type 'DbSet 1Proxy_4' which inherits from 'DbSet 1'. Test doubles for 'DbSet 1' must provide implementations of methods and properties that are used.
My class
public abstract class MoqBase<T> where T : DbContext
{
public Mock<T> MockContext { get; private set; }
public void Mock()
{
MockContext = new Mock<T> { DefaultValue = DefaultValue.Mock };
MockContext.SetupAllProperties();
PrepareContext();
}
private void PrepareContext()
{
var propertyList = MockContext.Object.GetType().GetProperties();
var tablePropertyList = propertyList.Where(x => x.PropertyType.FullName.Contains("DbSet")).ToList();
foreach (var tableProperty in tablePropertyList)
{
var proxy = tableProperty.GetValue(MockContext.Object);
dynamic mockSet = ((dynamic)proxy).Mock;
AddTableSetup(mockSet);
}
}
public void AddTableSetup<TTable>(Mock<DbSet<TTable>> Mockset) where TTable : class
{
var list = new List<TTable>();
var data = list.AsQueryable();
Mockset.As<IQueryable<TTable>>().Setup(m => m.Provider).Returns(data.Provider);
Mockset.As<IQueryable<TTable>>().Setup(m => m.Expression).Returns(data.Expression);
Mockset.As<IQueryable<TTable>>().Setup(m => m.ElementType).Returns(data.ElementType);
Mockset.As<IQueryable<TTable>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator);
}
}
TestMoq which implements MoqBase
public class TestMoq : MoqBase<NetConPortalContext>//NetConPortalContext is my dbContext
{
public TestMoq() => Mock();
}
Try to access
static void Main()
{
var moq = new TestMoq();
var context = moq.MockContext.Object;
var test = context.User.FirstOrDefault();
}
Picture of Error and Setup in Debug

Moq a concrete class method call

I've got a setup like this with a concrete class that is instantiated inside the method I want to test. I want to mock this concrete class an not have it execute the code inside. Hence, no exception should be thrown:
public class Executor
{
public bool ExecuteAction(ActionRequest request)
{
switch (request.ActionType)
{
case ActionType.Foo:
var a = new Foo();
return a.Execute(request);
case ActionType.Bar:
var b = new Bar();
return b.Execute(request);
}
return true;
}
}
public class Foo
{
public virtual bool Execute(ActionRequest request)
{
throw new NotImplementedException();
}
}
public class Bar
{
public virtual bool Execute(ActionRequest request)
{
throw new NotImplementedException();
}
}
My NUnit test looks like this:
[Test]
public void GivenARequestToFooShouldExecuteFoo()
{
var action = new Mock<Foo>();
action.Setup(x => x.Execute(It.IsAny<ActionRequest>())).Returns(true);
var sut = new Mock<Executor>();
sut.Object.ExecuteAction(new ActionRequest
{
ActionType = ActionType.Foo
});
}
[Test]
public void GivenARequestToBarShouldExecuteBar()
{
var action = new Mock<Bar>();
action.Setup(x => x.Execute(It.IsAny<ActionRequest>())).Returns(true);
var sut = new Mock<Executor>();
sut.Object.ExecuteAction(new ActionRequest
{
ActionType = ActionType.Bar
});
}
I fiddled around with CallBase, but it didn't get me anywhere. Is there anyway I can solve this easily without dependency injection of these classes and adding interfaces? Is this possible just using Moq?
The only thing I can think to do currently is move the Execute methods into the Executor class and rename them to ExecuteFoo() and ExecuteBar(), but I have a lot of code to move so they'd have to be partial classes (sub classes?).
The problem is not with the mocking of the method but with the creation of the concrete class. The creation of Foo and Bar need to be inverted out of the Executor. It is responsible for executing the action, not creating it. with that this interface was created to handle the creation.
public interface IActionCollection : IDictionary<ActionType, Func<IExecute>> {
}
think of this as a collection of factories or a collection of creation strategies.
A common interface was created for the actions.
public interface IExecute {
bool Execute(ActionRequest request);
}
public class Foo : IExecute {
public virtual bool Execute(ActionRequest request) {
throw new NotImplementedException();
}
}
public class Bar : IExecute {
public virtual bool Execute(ActionRequest request) {
throw new NotImplementedException();
}
}
And the Executor was refactored to use dependency inversion.
public class Executor {
readonly IActionCollection factories;
public Executor(IActionCollection factories) {
this.factories = factories;
}
public bool ExecuteAction(ActionRequest request) {
if (factories.ContainsKey(request.ActionType)) {
var action = factories[request.ActionType]();
return action.Execute(request);
}
return false;
}
}
With that refactor done the Executor can be tested with fake actions.
public void GivenARequestToFooShouldExecuteFoo() {
//Arrange
var expected = true;
var key = ActionType.Foo;
var action = new Mock<Foo>();
action.Setup(x => x.Execute(It.IsAny<ActionRequest>())).Returns(expected);
var actions = new Mock<IActionCollection>();
actions.Setup(_ => _[key]).Returns(() => { return () => action.Object; });
actions.Setup(_ => _.ContainsKey(key)).Returns(true);
var sut = new Executor(actions.Object);
var request = new ActionRequest {
ActionType = ActionType.Foo
};
//Act
var actual = sut.ExecuteAction(request);
//Assert
Assert.AreEqual(expected, actual);
}
A production implementation of the factory collection can look like this
public class ActionCollection : Dictionary<ActionType, Func<IExecute>>, IActionCollection {
public ActionCollection()
: base() {
}
}
and configured accordingly with your concrete types.
var factories = ActionCollection();
factories[ActionType.Foo] = () => new Foo();
factories[ActionType.Bar] = () => new Bar();

How to register dependencies that are not defined explicitly as Strict using AutoFixture with FakeItEasy?

I use AutoFixture with FakeItEasy when I need to test a class with many dependencies, but I ned to mock only some of them. All the rest of dependencies I prefer mocking with Strict() option of FakeItEasy. In order to make my test more clean, I would like to mock only the dependencies that I want and be able to specify that all the dependencies that do not mock are created with Strict().
In the example below, I would like to be able to remove the two lines of code that create mock of IDependency2, but keep the same behavior: if the class under test accesses any method of IDependency2, an exception would be thrown.
Any idea how to do this?
[TestFixture]
public class AutoTests
{
[Test]
public void Test()
{
// Arrange
IFixture fixture = new Fixture().Customize(new AutoFakeItEasyCustomization());
var dependency1 = fixture.Freeze<IDependency1>();
A.CallTo(() => dependency1.DoSomething()).Returns(12);
// I do not want to specify these two lines
var dependency2 = A.Fake<IDependency2>(s=>s.Strict());
fixture.Register(() => dependency2);
// Act
var classUnderTest = fixture.Create<ClassUnderTest>();
var actualResult = classUnderTest.MethodUnderTest();
// Assert
Assert.That(actualResult,Is.GreaterThan(0));
}
}
public class ClassUnderTest
{
private readonly IDependency1 _dependency1;
private readonly IDependency2 _dependency2;
public ClassUnderTest(IDependency1 dependency1, IDependency2 dependency2)
{
_dependency1 = dependency1;
_dependency2 = dependency2;
}
public int MethodUnderTest()
{
return _dependency1.DoSomething()
+ _dependency2.DoSomething();
}
}
public interface IDependency1
{
int DoSomething();
}
public interface IDependency2
{
int DoSomething();
}
As a tradeoff solution, It's quite simple to implement custom ISpecimenBuilder to have all autogenerated fakes as Strict fakes. You can take a look at standard Ploeh.AutoFixture.AutoFakeItEasy.FakeItEasyRelay fakes builder to get idea what's going on behind the curtain. The modified custom builder for Strict fakes is implemented as follows:
public class FakeItEasyStrictRelay : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
if (context == null)
throw new ArgumentNullException("context");
if (!this.IsSatisfiedBy(request))
return new NoSpecimen(request);
var type = request as Type;
if (type == null)
return new NoSpecimen(request);
var fakeFactoryMethod = this.GetType()
.GetMethod("CreateStrictFake", BindingFlags.Instance | BindingFlags.NonPublic)
.MakeGenericMethod((Type) request);
var fake = fakeFactoryMethod.Invoke(this, new object[0]);
return fake;
}
public bool IsSatisfiedBy(object request)
{
var t = request as Type;
return (t != null) && ((t.IsAbstract) || (t.IsInterface));
}
private T CreateStrictFake<T>()
{
return A.Fake<T>(s => s.Strict());
}
}
It can be simply register with the following statement:
IFixture fixture = new Fixture().Customize(
new AutoFakeItEasyCustomization( new FakeItEasyStrictRelay()));

How to create a customization that omits the auto properties for a whole range of types?

I am trying to create a customization that allows me to specify that the properties of types that are not in a certain namespace should not be populated.
Basically, I am trying to change this:
fixture.Customize<Window>(c => c.OmitAutoProperties());
fixture.Customize<ContentControl>(c => c.OmitAutoProperties());
fixture.Customize<TextBlock>(c => c.OmitAutoProperties());
// Many many more...
to this:
fixture.Customize(t => !t.Namespace.StartsWith("MyProject"),
c => c.OmitAutoProperties());
How to achieve this?
I actually only care for the result, not for the fictious API shown here, so implementing my own ISpecimenBuilder or ICustomization is not a problem.
The easiest way to do this is probably through a custom specimen builder:
public class OmitPropertyForTypeInNamespace : ISpecimenBuilder
{
private readonly string ns;
public OmitPropertyForTypeInNamespace(string ns)
{
this.ns = ns;
}
public object Create(object request, ISpecimenContext context)
{
if (IsProperty(request) &&
IsDeclaringTypeInNamespace((PropertyInfo)request))
{
return new OmitSpecimen();
}
return new NoSpecimen(request);
}
private bool IsProperty(object request)
{
return request is PropertyInfo;
}
private bool IsDeclaringTypeInNamespace(PropertyInfo property)
{
var declaringType = property.DeclaringType;
return declaringType.Namespace.Equals(
this.ns,
StringComparison.OrdinalIgnoreCase);
}
}
As usual, it's a good convention to also provide a matching customization:
public class OmitAutoPropertiesForTypesInNamespace : ICustomization
{
private readonly string ns;
public OmitAutoPropertiesForTypesInNamespace(string ns)
{
this.ns = ns;
}
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(new OmitPropertyForTypeInNamespace(this.ns));
}
}
This will ultimately allow you to say:
var fixture = new Fixture();
fixture.Customize(new OmitAutoPropertiesForTypesInNamespace("MyProject"));

Creating a hybrid of a mock and an anonymous object using e.g. Moq and AutoFixture?

I encountered a class during my work that looks like this:
public class MyObject
{
public int? A {get; set;}
public int? B {get; set;}
public int? C {get; set;}
public virtual int? GetSomeValue()
{
//simplified behavior:
return A ?? B ?? C;
}
}
The issue is that I have some code that accesses A, B and C and calls the GetSomeValue() method (now, I'd say this is not a good design, but sometimes my hands are tied ;-)). I want to create a mock of this object, which, at the same time, has A, B and C set to some values. So, when I use moq as such:
var m = new Mock<MyObject>() { DefaultValue = DefaultValue.Mock };
lets me setup a result on GetSomeValue() method, but all the properties are set to null (and setting up all of them using Setup() is quite cumbersome, since the real object is a nasty data object and has more properties than in above simplified example).
So on the other hand, using AutoFixture like this:
var fixture = new Fixture();
var anyMyObject = fixture.CreateAnonymous<MyObject>();
Leaves me without the ability to stup a call to GetSomeValue() method.
Is there any way to combine the two, to have anonymous values and the ability to setup call results?
Edit
Based on nemesv's answer, I derived the following utility method (hope I got it right):
public static Mock<T> AnonymousMock<T>() where T : class
{
var mock = new Mock<T>();
fixture.Customize<T>(c => c.FromFactory(() => mock.Object));
fixture.CreateAnonymous<T>();
fixture.Customizations.RemoveAt(0);
return mock;
}
This is actually possible to do with AutoFixture, but it does require a bit of tweaking. The extensibility points are all there, but I admit that in this case, the solution isn't particularly discoverable.
It becomes even harder if you want it to work with nested/complex types.
Given the MyObject class above, as well as this MyParent class:
public class MyParent
{
public MyObject Object { get; set; }
public string Text { get; set; }
}
these unit tests all pass:
public class Scenario
{
[Fact]
public void CreateMyObject()
{
var fixture = new Fixture().Customize(new MockHybridCustomization());
var actual = fixture.CreateAnonymous<MyObject>();
Assert.NotNull(actual.A);
Assert.NotNull(actual.B);
Assert.NotNull(actual.C);
}
[Fact]
public void MyObjectIsMock()
{
var fixture = new Fixture().Customize(new MockHybridCustomization());
var actual = fixture.CreateAnonymous<MyObject>();
Assert.NotNull(Mock.Get(actual));
}
[Fact]
public void CreateMyParent()
{
var fixture = new Fixture().Customize(new MockHybridCustomization());
var actual = fixture.CreateAnonymous<MyParent>();
Assert.NotNull(actual.Object);
Assert.NotNull(actual.Text);
Assert.NotNull(Mock.Get(actual.Object));
}
[Fact]
public void MyParentIsMock()
{
var fixture = new Fixture().Customize(new MockHybridCustomization());
var actual = fixture.CreateAnonymous<MyParent>();
Assert.NotNull(Mock.Get(actual));
}
}
What's in MockHybridCustomization? This:
public class MockHybridCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(
new MockPostprocessor(
new MethodInvoker(
new MockConstructorQuery())));
fixture.Customizations.Add(
new Postprocessor(
new MockRelay(t =>
t == typeof(MyObject) || t == typeof(MyParent)),
new AutoExceptMoqPropertiesCommand().Execute,
new AnyTypeSpecification()));
}
}
The MockPostprocessor, MockConstructorQuery and MockRelay classes are defined in the AutoMoq extension to AutoFixture, so you'll need to add a reference to this library. However, note that it's not required to add the AutoMoqCustomization.
The AutoExceptMoqPropertiesCommand class is also custom-built for the occasion:
public class AutoExceptMoqPropertiesCommand : AutoPropertiesCommand<object>
{
public AutoExceptMoqPropertiesCommand()
: base(new NoInterceptorsSpecification())
{
}
protected override Type GetSpecimenType(object specimen)
{
return specimen.GetType();
}
private class NoInterceptorsSpecification : IRequestSpecification
{
public bool IsSatisfiedBy(object request)
{
var fi = request as FieldInfo;
if (fi != null)
{
if (fi.Name == "__interceptors")
return false;
}
return true;
}
}
}
This solution provides a general solution to the question. However, it hasn't been extensively tested, so I'd love to get feedback on it.
Probably there is a better why, but this works:
var fixture = new Fixture();
var moq = new Mock<MyObject>() { DefaultValue = DefaultValue.Mock };
moq.Setup(m => m.GetSomeValue()).Returns(3);
fixture.Customize<MyObject>(c => c.FromFactory(() => moq.Object));
var anyMyObject = fixture.CreateAnonymous<MyObject>();
Assert.AreEqual(3, anyMyObject.GetSomeValue());
Assert.IsNotNull(anyMyObject.A);
//...
Initially I tried to use fixture.Register(() => moq.Object); instead of fixture.Customize but it registers the creator function with OmitAutoProperties() so it wouldn't work for you case.
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.
var fixture = new Fixture().Customize(new AutoConfiguredMoqCustomization());
var mock = fixture.Create<Mock<MyObject>>();
Assert.NotNull(mock.Object.A);
Assert.NotNull(mock.Object.B);
Assert.NotNull(mock.Object.C);

Categories

Resources