C# 测试结构映射约定
我对StructureMap有如下约定:C# 测试结构映射约定,c#,unit-testing,structuremap,C#,Unit Testing,Structuremap,我对StructureMap有如下约定: public class FakeRepositoriesConvention : IRegistrationConvention { public void Process(Type type, global::StructureMap.Configuration.DSL.Registry registry) { if (type.Name.StartsWith("Fake") && type.Name.E
public class FakeRepositoriesConvention : IRegistrationConvention
{
public void Process(Type type, global::StructureMap.Configuration.DSL.Registry registry)
{
if (type.Name.StartsWith("Fake") && type.Name.EndsWith("Repository"))
{
string interfaceName = "I" + type.Name.Replace("Fake", String.Empty);
registry.AddType(type.GetInterface(interfaceName), type);
}
}
}
我想为此实现单元测试,但我不知道如何实现
我的第一个想法是发送一个模拟注册表,然后测试AddType()是否使用正确的参数调用。但我无法让它工作,可能是因为AddType()不是虚拟的。注册表实现IRegistry,但这对我没有帮助,因为Process方法不接受接口
所以我的问题是-我如何测试它?
(我正在使用nUnit和Rhinomock)您可以完全跳过模拟,使用简化版本的注册表和组件以及预定义的虚拟类型:
// Dummy types for test usage only
public interface ICorrectRepository { }
public class FakeCorrectRepository : ICorrectRepository { }
[Test]
Process_RegistersFakeRepositoryType_ThroughInterfaceTypeName()
{
var registry = new Registry();
var convention = new FakeRepositoriesConvention();
// exercise test
convention.Process(typeof(FakeCorrectRepository), registry);
// assert it worked
var container = new Container(c => c.AddRegistry(registry));
var instance = container.GetInstance<ICorrectRepository>();
Assert.That(instance, Is.Not.Null);
}
//虚拟类型仅用于测试
公共接口ICorrectRepository{}
公共类FakeCorrectRepository:ICorrectRepository{}
[测试]
通过InterFaceTypeName()处理\u注册表FakerRepositoryType\u
{
var registry=new registry();
var约定=新的fakerepositoriescovention();
//运动试验
公约.程序(类型(伪造更正储存库),登记处);
//断言它起作用了
var容器=新容器(c=>c.AddRegistry(注册表));
var instance=container.GetInstance();
Assert.That(实例,Is.Not.Null);
}
若你们的约定和你们假设的一样有效,那个么上面的测试应该通过