Skip to content

Commit 23ad8de

Browse files
Fixed the issue where a non-virtual property on an entity would throw an
exception in ProxyBuilder
1 parent f6c2849 commit 23ad8de

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace NTestDataBuilder.Tests.Entities
2+
{
3+
public class Company
4+
{
5+
protected Company() { }
6+
7+
public Company(string name, int employeeCount)
8+
{
9+
Name = name;
10+
EmployeeCount = employeeCount;
11+
}
12+
13+
public virtual string Name { get; private set; }
14+
public int EmployeeCount { get; private set; }
15+
}
16+
}

NTestDataBuilder.Tests/NTestDataBuilder.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<Compile Include="Builders\ProxyAlteringCustomerBuilder.cs" />
5555
<Compile Include="BuildTests.cs" />
5656
<Compile Include="CreateListTests.cs" />
57+
<Compile Include="Entities\Company.cs" />
5758
<Compile Include="Entities\Customer.cs" />
5859
<Compile Include="GetOrDefaultTests.cs" />
5960
<Compile Include="GetSetTests.cs" />

NTestDataBuilder.Tests/ProxyBuilderTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,21 @@ public void GivenClassToProxyWithMultiplePropertyValues_WhenBuildingProxy_Return
5959
Assert.That(proxy.LastName, Is.EqualTo("LastName"));
6060
Assert.That(proxy.YearJoined, Is.EqualTo(1));
6161
}
62+
63+
[Test]
64+
public void GivenClassWithSomeVirtualProperties_WhenBuildingProxy_ThenOnlyVirtualMembersAreProxied()
65+
{
66+
var proxyBuilder = new ProxyBuilder<Company>(new Dictionary<string, object>()
67+
{
68+
{"Name", "Vandelay Industries"},
69+
{"EmployeeCount", 100}
70+
});
71+
72+
var proxy = proxyBuilder.Build();
73+
74+
Assert.That(proxy.Name, Is.EqualTo("Vandelay Industries"));
75+
Assert.That(proxy.EmployeeCount, Is.EqualTo(0));
76+
}
6277
}
6378
}
79+

NTestDataBuilder/ProxyBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public T Build()
3232
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
3333
foreach (var property in properties.Where(property => _properties.ContainsKey(property.Name)))
3434
{
35-
property.GetValue(proxy, null).Returns(_properties[property.Name]);
35+
if (property.GetGetMethod().IsVirtual)
36+
property.GetValue(proxy, null).Returns(_properties[property.Name]);
3637
}
3738

3839
return proxy;

0 commit comments

Comments
 (0)