I have such a class:
public class ViewModel
{
public IPagination<Data> List { get; set; } // interface!
public SearchFilter SearchFilter { get; set; }
public string Test { get; set; }
}
public class SearchFilter
{
public string Name { get; set; }
}
A dynamic proxy shall be created around the IPagination interface and the proxy shall be filled with test data.
Now is it possible to let AutoFixture create an instance of the ViewModel type?
Be aware that I only know the type at runtime (typeof(ViewModel)
).
By now I know I can do this:
var context = new SpecimenContext(fixture.Compose());
var value = context.Resolve(new SeededRequest(typeof(ViewModel), null))
A simple possibility is to register a factory method:
To let AutoFixture automatically create proxies for interfaces you want to use one of the auto-mocking customizations:
In your concrete case, this will create an instance of the interface representing an empty list.
When you want to have test data in that list, you don't want to use the auto-mocking customization but a customization that understands the semantics of
IPagination
as described in this blog post.If that is too complex, you can still use the
Register
method:Theoretically, it should be possible to fill the properties of an auto-mocked instance.
Assuming that the
IPagination<T>
property of theViewModel
type is defined as:We could create an ad-hoc auto-mocking customization, e.g.
MyCustomization
.Then, the following call will create an instance of the
ViewModel
(which is known only at runtime), provide an auto-mocked instance of theIPagination<Data>
and assign values to the properties.MyCustomization