我正在尝试创建一个单元测试来验证我的加载AutoMapper配置的方法是否正常工作.我加载AutoMapper配置的想法是将配置调用放在具有以下接口的类中:
public interface IEntityMapConfiguration
{
void ConfigureMapping();
}
Run Code Online (Sandbox Code Playgroud)
要动态加载它们,我有以下加载器类
public class EntityMapLoader
{
public static void LoadEntityMappings()
{
// Load all IEntityMapConfiguration classes via reflection
var types = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(x => x.GetTypes())
.Where(x => x.IsSubclassOf(typeof(IEntityMapConfiguration)))
.Select(x => x as IEntityMapConfiguration)
.ToList();
foreach (var config in types)
config.ConfigureMapping();
}
}
Run Code Online (Sandbox Code Playgroud)
为了验证这一点,我创建了以下单元测试
public class UnitTestEntityViewModel
{
public int Id { get; set; }
public int Text2 { get; set; }
}
public class TestProjectionMapping : IEntityMapConfiguration
{
public void ConfigureMapping()
{
Mapper.CreateMap<UnitTestEntity, UnitTestEntityViewModel>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.Text2, opt => opt.MapFrom(src => src.Text));
}
}
[TestClass]
public class AutomapperConfigurationTests
{
[TestMethod]
public void Loader_Loads_All_Configurations_In_Assembly()
{
// Setup
UnitTestEntity entity = new UnitTestEntity { Id = 2, Text = "Test 1234" };
// Act
EntityMapLoader.LoadEntityMappings();
// Verify
UnitTestEntityViewModel result = Mapper.Map<UnitTestEntity, UnitTestEntityViewModel>(entity);
Assert.AreEqual(entity.Id, result.Id, "View model's ID value did not match");
Assert.AreEqual(entity.Text, result.Text2, "View model's text value did not match");
}
}
Run Code Online (Sandbox Code Playgroud)
问题是这个单元测试失败了,因为它没有拿起我的TestProjectionMapping
课,我无法弄清楚原因.在我LoadEntityMappings()
的types
列表中,计数为零.我已经验证我的测试程序集是从中返回的AppDomain.CurrentDomain.GetAssemblies()
.
我确定我做的事情显然是错的,但我找不到.有任何想法吗?
编辑:我更新了我的LoadentityMappings()
方法:
public static void LoadEntityMappings()
{
// Load all IEntityMapConfiguration classes via reflection
var types = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(x => x.GetTypes())
.Where(x => x.GetInterfaces().Contains(typeof(IEntityMapConfiguration)))
.Select(x => x as IEntityMapConfiguration)
.ToList();
foreach (var config in types)
config.ConfigureMapping();
}
Run Code Online (Sandbox Code Playgroud)
这仍然是失败的.声明(没有最终选择)正在返回我的TestProjectionMapping
课程,但是x as IEntityMapconfiguration
失败了.在我键入的调试器中,我(IEntityMapConfiguration)types[0]
收到以下错误:
Cannot cast 'types[0]' (which has an actual type of 'System.RuntimeType') to 'MyJobLeads.DomainModel.EntityMapping.IEntityMapConfiguration'
在观察列表中输入以供参考types[0]
:
types[0] {Name = "TestProjectionMapping" FullName = "MyJobLeads.Tests.TestProjectionMapping"} System.Type {System.RuntimeType}
还有,types[0] is IEntityMapConfiguration
是false
我不知道为什么我不能做这个演员,任何想法?
您无法使用Type.IsSubclassOf
查看类型是否实现接口.您必须使用Type.IsAssignableFrom
(或者您可以使用is IEntityMapConfiguration
或Type.GetInterfaces
然后查看结果序列是否包含typeof(IEntityMapConfiguration)
.
现在想想为什么你不能Type.IsSubclassOf
用来看看是否Type
实现了一个接口.因为我们没有说实现接口的类是该接口的子类; 相反,我们说它实现了接口,我们保留它的基类的子类(object
或者ValueType
除非指定了一个).
顺便说一句,这在文档中明确说明Type.IsSubclassOf
:
该
IsSubclassOf
方法不能用于确定接口是否从另一个接口派生,或者类是否实现接口.
归档时间: |
|
查看次数: |
241 次 |
最近记录: |