AutoFixture.AutoMoq提供一个已知值一个构造函数的参数
我刚刚开始使用 AutoFixture.AutoMoq 在我的单元测试,我发现,用于创建,我并不关心具体的值对象,它非常有帮助。毕竟,匿名对象的创建是它的全部。
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.
当我关心的构造函数的参数中的一个或多个我正在挣扎的。以 ExampleComponent
如下:
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)
{
}
}
我想写一个测试,我为 someValue中
提供一个特定的值,但将 IService
来可以通过自动生成的 AutoFixture.AutoMoq
I want to write a test where I supply a specific value for someValue
but leave IService
to be created automatically by AutoFixture.AutoMoq.
我知道如何使用冻结
上我的 IFixture
来保持一个已知值将被注入到一个组件的电话,但我不能完全看到怎样的电源的一个已知的值。我自己
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
...
}
我知道我可以通过直接调用构造函数做到这一点,但是这将不再是匿名的对象的创建。有没有使用 AutoFixture.AutoMock 这样的方式还是我需要把一个DI容器到我的测试,能够做我想要什么?
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?
编辑:
我也许应该已经在我原来的问题少absract所以这里是我的具体情况。
I probably should have been less absract in my original question so here is my specific scenario.
我有一个具有通用的 ICACHE
接口 TryRead< ; T>
和写< T>
方法:
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...
}
我实施 CookieCache
,其中 ITypeConverter
处理的对象转换成与字符串和寿命
用来设置cookie的到期日
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...
}
因此,对于一个cookie的到期日编写测试的时候,我关心的寿命,但没有这么多的转换器。
So when writing a test for the expiry date of a cookie, I care about the lifespan but not so much about the converter.
您必须更换:
string knownValue = fixture.Freeze<string>("My known value");
与
with:
fixture.Inject("My known value");
您可以阅读更多关于注入
这里。
You can read more about Inject
here.
其实,冻结
扩展方法做:
var value = fixture.Create<T>();
fixture.Inject(value);
return value;
这意味着你在测试中居然叫创建中所使用的过载; T> ;
用的种子:我的已知值的造成的我知value4d41f94f-1fc9-4115-9f29-e50bc2b4ba5e的。
Which means that the overload you used in the test actually called Create<T>
with a seed: My known value resulting in "My known value4d41f94f-1fc9-4115-9f29-e50bc2b4ba5e".