Skip to content

Commit 72c4c1c

Browse files
authored
Add unit test for DynamicClass SerializeToJson (#641)
1 parent d0a3d9a commit 72c4c1c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

test/System.Linq.Dynamic.Core.Tests/DynamicClassTest.cs

+24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Collections.Generic;
2+
using System.Xml.Linq;
23
using FluentAssertions;
4+
using Newtonsoft.Json;
35
using Xunit;
46

57
namespace System.Linq.Dynamic.Core.Tests
@@ -98,5 +100,27 @@ public void DynamicClass_SettingNewProperty_ByIndex_Should_Work()
98100
var value = item["X"] as string;
99101
value.Should().Be(newTest);
100102
}
103+
104+
[Fact]
105+
public void DynamicClass_SerializeToJson_Should_Work()
106+
{
107+
// Arrange
108+
var props = new[]
109+
{
110+
new DynamicProperty("Name", typeof(string)),
111+
new DynamicProperty("Birthday", typeof(DateTime))
112+
};
113+
var type = DynamicClassFactory.CreateType(props);
114+
115+
var dynamicClass = (DynamicClass) Activator.CreateInstance(type);
116+
dynamicClass.SetDynamicPropertyValue("Name", "Albert");
117+
dynamicClass.SetDynamicPropertyValue("Birthday", new DateTime(1879, 3, 14));
118+
119+
// Act
120+
var json = JsonConvert.SerializeObject(dynamicClass);
121+
122+
// Assert
123+
json.Should().Be("{\"Name\":\"Albert\",\"Birthday\":\"1879-03-14T00:00:00\"}");
124+
}
101125
}
102126
}

0 commit comments

Comments
 (0)