我刚刚开始在我的单元测试中使用 AutoFixture.AutoMoq,我发现它对于创建我不关心具体值的对象非常有帮助。毕竟,匿名对象创建就是它的全部。
当我关心一个或多个构造函数参数时,我遇到了困难。取ExampleComponent
下面:
public class ExampleComponent
{
public ExampleComponent(IService service, string someValue)
{
}
}
我想编写一个测试,为 someValue
提供特定值但离开IService
由 AutoFixture.AutoMoq 自动创建。
我知道如何使用 Freeze
在我的 IFixture
保留将被注入(inject)到组件中的已知值,但我不太明白如何提供我自己的已知值。
这是我最想做的事:
[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 容器合并到我的测试中才能执行我想要的操作?
编辑:
我最初的问题可能应该不那么抽象,所以这是我的具体情况。
我有一个 ICache
具有通用 TryRead<T>
的接口(interface)和 Write<T>
方法:
public interface ICache
{
bool TryRead<T>(string key, out T value);
void Write<T>(string key, T value);
// other methods not shown...
}
我正在实现 CookieCache
其中 ITypeConverter
处理对象与字符串之间的转换和 lifespan
用于设置 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 的到期日期编写测试时,我关心的是生命周期而不是转换器。
最佳答案
所以我确信人们可以制定出 Mark 的建议的一般实现,但我想我会发布它以征求意见。
我基于 Mark 的 LifeSpanArg
创建了一个通用的 ParameterNameSpecimenBuilder
:
public class ParameterNameSpecimenBuilder<T> : ISpecimenBuilder
{
private readonly string name;
private readonly T value;
public ParameterNameSpecimenBuilder(string name, T value)
{
// we don't want a null name but we might want a null value
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentNullException("name");
}
this.name = name;
this.value = value;
}
public object Create(object request, ISpecimenContext context)
{
var pi = request as ParameterInfo;
if (pi == null)
{
return new NoSpecimen(request);
}
if (pi.ParameterType != typeof(T) ||
!string.Equals(
pi.Name,
this.name,
StringComparison.CurrentCultureIgnoreCase))
{
return new NoSpecimen(request);
}
return this.value;
}
}
然后我在 IFixture
上定义了一个通用的 FreezeByName
扩展方法来设置自定义:
public static class FreezeByNameExtension
{
public static void FreezeByName<T>(this IFixture fixture, string name, T value)
{
fixture.Customizations.Add(new ParameterNameSpecimenBuilder<T>(name, value));
}
}
下面的测试现在将通过:
[TestMethod]
public void FreezeByName_Sets_Value1_And_Value2_Independently()
{
//// Arrange
IFixture arrangeFixture = new Fixture();
string myValue1 = arrangeFixture.Create<string>();
string myValue2 = arrangeFixture.Create<string>();
IFixture sutFixture = new Fixture();
sutFixture.FreezeByName("value1", myValue1);
sutFixture.FreezeByName("value2", myValue2);
//// Act
TestClass<string> result = sutFixture.Create<TestClass<string>>();
//// Assert
Assert.AreEqual(myValue1, result.Value1);
Assert.AreEqual(myValue2, result.Value2);
}
public class TestClass<T>
{
public TestClass(T value1, T value2)
{
this.Value1 = value1;
this.Value2 = value2;
}
public T Value1 { get; private set; }
public T Value2 { get; private set; }
}
关于c# - AutoFixture.AutoMoq 为一个构造函数参数提供一个已知值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16819470/