I've just started to use AutoFixture.AutoMoq in my unit tests and I'm finding it very helpful for creating objects where I don't care about the specific value. After all, anonymous object creation is what it is all about.
What I'm struggling with is when I care about one or more of the constructor parameters. Take ExampleComponent
below:
public class ExampleComponent
{
public ExampleComponent(IService service, string someValue)
{
}
}
I want to write a test where I supply a specific value for someValue
but leave IService
to be created automatically by AutoFixture.AutoMoq.
I know how to use Freeze
on my IFixture
to keep hold of a known value that will be injected into a component but I can't quite see how to supply a known value of my own.
Here is what I would ideally like to do:
[TestMethod]
public void Create_ExampleComponent_With_Known_SomeValue()
{
// create a fixture that supports automocking
IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());
// supply a known value for someValue (this method doesn't exist)
string knownValue = fixture.Freeze<string>("My known value");
// create an ExampleComponent with my known value injected
// but without bothering about the IService parameter
ExampleComponent component = this.fixture.Create<ExampleComponent>();
// exercise component knowning it has my known value injected
...
}
I know I could do this by calling the constructor directly but this would no longer be anonymous object creation. Is there a way to use AutoFixture.AutoMock like this or do I need to incorporate a DI container into my tests to be able to do what I want?
EDIT:
I probably should have been less absract in my original question so here is my specific scenario.
I have an ICache
interface which has generic TryRead<T>
and Write<T>
methods:
public interface ICache
{
bool TryRead<T>(string key, out T value);
void Write<T>(string key, T value);
// other methods not shown...
}
I'm implementing a CookieCache
where ITypeConverter
handles converting objects to and from strings and lifespan
is used to set the expiry date of a cookie.
public class CookieCache : ICache
{
public CookieCache(ITypeConverter converter, TimeSpan lifespan)
{
// usual storing of parameters
}
public bool TryRead<T>(string key, out T result)
{
// read the cookie value as string and convert it to the target type
}
public void Write<T>(string key, T value)
{
// write the value to a cookie, converted to a string
// set the expiry date of the cookie using the lifespan
}
// other methods not shown...
}
So when writing a test for the expiry date of a cookie, I care about the lifespan but not so much about the converter.
ExampleComponent
. There's a reason AutoFixture doesn't support this out of the box. – Adelinelifespan
. Doesn'tlifespan
have to interact with the current time? Once you start to think about questions such as those, perhaps an abstraction still emerges. The last time I did something like this, I arrived at an ILease interface instead, which made the cache logic much more flexible because I could now support: Absolute Expiry, Sliding Window Expiry, LRU Expiry, and lots of other options. – AdelineIDateTimeProvider
dependency to myCookieCache
and I set the expiry date of a cookie by adding thelifespan
to the current date. I realise now that this really is a mixed concern, even though that mixed concern only took one line of code! – Ungracious