DTO runtime build unit test

pull/1/head
Ogoun 6 years ago
parent 75a6a7b2a8
commit fdff425bf1

@ -0,0 +1,39 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using ZeroLevel.Services.ObjectMapping;
using ZeroLevel.Services.Reflection;
namespace ZeroLevel.ReflectionUnitTests
{
[TestClass]
public class ReflectionTests
{
[TestMethod]
public void TestDTORuntymeBuildedTypes()
{
// Arrange
var date = DateTime.Now;
var typeBuilder = new DTOTypeBuilder("MyType");
typeBuilder.AppendField<string>("Title");
typeBuilder.AppendProperty<long>("Id");
typeBuilder.AppendProperty<DateTime>("Created");
var type = typeBuilder.Complete();
// Act
var mapper = TypeMapper.Create(type);
var instance = TypeHelpers.CreateNonInitializedInstance(type);
mapper.Set(instance, "Title", "This is title");
mapper.Set(instance, "Id", 1001001);
mapper.Set(instance, "Created", date);
// Assert
Assert.IsTrue(mapper.Exists("Title"));
Assert.IsTrue(mapper.Exists("Id"));
Assert.IsTrue(mapper.Exists("Created"));
Assert.AreEqual(mapper.Get(instance, "Id"), (long)1001001);
Assert.AreEqual(mapper.Get(instance, "Created"), date);
Assert.AreEqual(mapper.Get(instance, "Title"), "This is title");
}
}
}

@ -70,6 +70,7 @@
<Compile Include="Models\TestDTO.cs" /> <Compile Include="Models\TestDTO.cs" />
<Compile Include="PredicateBuilderTests.cs" /> <Compile Include="PredicateBuilderTests.cs" />
<Compile Include="QueriesTests.cs" /> <Compile Include="QueriesTests.cs" />
<Compile Include="ReflectionTests.cs" />
<Compile Include="SerializationTests.cs" /> <Compile Include="SerializationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SpecificationPatternTest.cs" /> <Compile Include="SpecificationPatternTest.cs" />

@ -185,5 +185,14 @@ namespace ZeroLevel.Services.Reflection
return Activator.CreateInstance(type); return Activator.CreateInstance(type);
return FormatterServices.GetUninitializedObject(type); return FormatterServices.GetUninitializedObject(type);
} }
public static object CreateNonInitializedInstance(Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
return FormatterServices.GetUninitializedObject(type);
}
} }
} }
Loading…
Cancel
Save

Powered by TurnKey Linux.