Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions NTestDataBuilder.Tests/Entities/Company.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace NTestDataBuilder.Tests.Entities
{
public class Company
{
protected Company() { }

public Company(string name, int employeeCount)
{
Name = name;
EmployeeCount = employeeCount;
}

public virtual string Name { get; private set; }
public int EmployeeCount { get; private set; }
}
}
1 change: 1 addition & 0 deletions NTestDataBuilder.Tests/NTestDataBuilder.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<Compile Include="Builders\ProxyAlteringCustomerBuilder.cs" />
<Compile Include="BuildTests.cs" />
<Compile Include="CreateListTests.cs" />
<Compile Include="Entities\Company.cs" />
<Compile Include="Entities\Customer.cs" />
<Compile Include="GetOrDefaultTests.cs" />
<Compile Include="GetSetTests.cs" />
Expand Down
16 changes: 16 additions & 0 deletions NTestDataBuilder.Tests/ProxyBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,21 @@ public void GivenClassToProxyWithMultiplePropertyValues_WhenBuildingProxy_Return
Assert.That(proxy.LastName, Is.EqualTo("LastName"));
Assert.That(proxy.YearJoined, Is.EqualTo(1));
}

[Test]
public void GivenClassWithSomeVirtualProperties_WhenBuildingProxy_ThenOnlyVirtualMembersAreProxied()
{
var proxyBuilder = new ProxyBuilder<Company>(new Dictionary<string, object>()
{
{"Name", "Vandelay Industries"},
{"EmployeeCount", 100}
});

var proxy = proxyBuilder.Build();

Assert.That(proxy.Name, Is.EqualTo("Vandelay Industries"));
Assert.That(proxy.EmployeeCount, Is.EqualTo(0));
}
}
}

3 changes: 2 additions & 1 deletion NTestDataBuilder/ProxyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public T Build()
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties.Where(property => _properties.ContainsKey(property.Name)))
{
property.GetValue(proxy, null).Returns(_properties[property.Name]);
if (property.GetGetMethod().IsVirtual)
property.GetValue(proxy, null).Returns(_properties[property.Name]);
}

return proxy;
Expand Down