我刚开始在我的单元测试中使用AutoFixture.AutoMoq,我发现它对于创建我不关心特定值的对象非常有帮助.毕竟,匿名对象创建就是它的全部.
我正在努力的是当我关心一个或多个构造函数参数时.采取ExampleComponent
下面:
public class ExampleComponent
{
public ExampleComponent(IService service, string someValue)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我想写一个测试,我提供了一个特定的值,someValue
但是IService
由AutoFixture.AutoMoq自动创建.
我知道如何使用Freeze
我IFixture
来保持一个已注入组件的已知值,但我不太明白如何提供我自己的已知值.
这是我理想的做法:
[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 …
Run Code Online (Sandbox Code Playgroud) 我已经阅读了Mark Seeman 关于自动模拟的文章,我现在正在编写一个基于该文章的可重复使用的windsor容器.
我执行Mark的文章(基本上直接复制)
主要工作是在AutoMoqResolver
课堂上完成的.只要类依赖于接口,这将提供模拟:
public class AutoMoqResolver : ISubDependencyResolver
{
private readonly IKernel kernel;
public AutoMoqResolver(IKernel kernel)
{
this.kernel = kernel;
}
public bool CanResolve(
CreationContext context,
ISubDependencyResolver contextHandlerResolver,
ComponentModel model,
DependencyModel dependency)
{
return dependency.TargetType.IsInterface;
}
public object Resolve(
CreationContext context,
ISubDependencyResolver contextHandlerResolver,
ComponentModel model,
DependencyModel dependency)
{
var mockType = typeof(Mock<>).MakeGenericType(dependency.TargetType);
return ((Mock)this.kernel.Resolve(mockType)).Object;
}
}
Run Code Online (Sandbox Code Playgroud)
将AutoMoqResolver
被添加到使用以下实施的容器IWindsorInstaller
接口:
public class AutoMockInstaller<T> : IWindsorInstaller
{
public void Install(
IWindsorContainer container,
IConfigurationStore …
Run Code Online (Sandbox Code Playgroud)