Skip to content

Commit f81a80e

Browse files
committed
Added Proxy Builder
1 parent 606460e commit f81a80e

File tree

6 files changed

+116
-3
lines changed

6 files changed

+116
-3
lines changed

NTestDataBuilder.Tests/Entities/Customer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace NTestDataBuilder.Tests.Entities
44
{
55
public class Customer
66
{
7+
protected Customer() {}
8+
79
public Customer(string firstName, string lastName, int yearJoined)
810
{
911
if (string.IsNullOrEmpty(firstName))
@@ -23,8 +25,8 @@ public int CustomerForHowManyYears(DateTime since)
2325
return since.Year - YearJoined;
2426
}
2527

26-
public string FirstName { get; private set; }
27-
public string LastName { get; private set; }
28-
public int YearJoined { get; private set; }
28+
public virtual string FirstName { get; private set; }
29+
public virtual string LastName { get; private set; }
30+
public virtual int YearJoined { get; private set; }
2931
}
3032
}

NTestDataBuilder.Tests/NTestDataBuilder.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
<Reference Include="FizzWare.NBuilder">
3636
<HintPath>..\packages\NBuilder.3.0.1.1\lib\FizzWare.NBuilder.dll</HintPath>
3737
</Reference>
38+
<Reference Include="NSubstitute, Version=1.6.0.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca, processorArchitecture=MSIL">
39+
<SpecificVersion>False</SpecificVersion>
40+
<HintPath>..\packages\NSubstitute.1.6.0.0\lib\NET40\NSubstitute.dll</HintPath>
41+
</Reference>
3842
<Reference Include="nunit.framework">
3943
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
4044
</Reference>
@@ -52,6 +56,7 @@
5256
<Compile Include="GetOrDefaultTests.cs" />
5357
<Compile Include="GetSetTests.cs" />
5458
<Compile Include="Properties\AssemblyInfo.cs" />
59+
<Compile Include="ProxyBuilderTests.cs" />
5560
</ItemGroup>
5661
<ItemGroup>
5762
<None Include="packages.config" />
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using NSubstitute;
4+
using NTestDataBuilder.Tests.Entities;
5+
using NUnit.Framework;
6+
7+
namespace NTestDataBuilder.Tests
8+
{
9+
class ProxyBuilderTests
10+
{
11+
[Test]
12+
public void GivenClassToProxyWithNoProperties_WhenBuildingProxy_ReturnAClassWithNoReturnsValuesSet()
13+
{
14+
var proxyBuilder = new ProxyBuilder<Customer>(new Dictionary<string, object>());
15+
16+
var proxy = proxyBuilder.Build();
17+
18+
Assert.That(proxy.FirstName, Is.EqualTo(string.Empty));
19+
Assert.That(proxy.LastName, Is.EqualTo(string.Empty));
20+
Assert.That(proxy.YearJoined, Is.EqualTo(0));
21+
}
22+
23+
[Test]
24+
public void GivenClassToProxyWithNoProperties_WhenBuildingProxy_ReturnAnNSubstituteProxyOfThatClass()
25+
{
26+
var proxyBuilder = new ProxyBuilder<Customer>(new Dictionary<string, object>());
27+
28+
var proxy = proxyBuilder.Build();
29+
30+
proxy.DidNotReceive().CustomerForHowManyYears(Arg.Any<DateTime>());
31+
}
32+
33+
[Test]
34+
public void GivenClassToProxyWithSinglePropertyValue_WhenBuildingProxy_ReturnAClassWithReturnValueSet()
35+
{
36+
var proxyBuilder = new ProxyBuilder<Customer>(new Dictionary<string, object> {{"FirstName", "FirstName"}});
37+
38+
var proxy = proxyBuilder.Build();
39+
40+
Assert.That(proxy.FirstName, Is.EqualTo("FirstName"));
41+
Assert.That(proxy.LastName, Is.EqualTo(string.Empty));
42+
Assert.That(proxy.YearJoined, Is.EqualTo(0));
43+
}
44+
45+
[Test]
46+
public void GivenClassToProxyWithMultiplePropertyValues_WhenBuildingProxy_ReturnAClassWithReturnValueSet()
47+
{
48+
var proxyBuilder = new ProxyBuilder<Customer>(new Dictionary<string, object>
49+
{
50+
{ "FirstName", "FirstName" },
51+
{ "LastName", "LastName" },
52+
{ "YearJoined", 1 },
53+
}
54+
);
55+
56+
var proxy = proxyBuilder.Build();
57+
58+
Assert.That(proxy.FirstName, Is.EqualTo("FirstName"));
59+
Assert.That(proxy.LastName, Is.EqualTo("LastName"));
60+
Assert.That(proxy.YearJoined, Is.EqualTo(1));
61+
}
62+
}
63+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="NBuilder" version="3.0.1.1" targetFramework="net40" />
4+
<package id="NSubstitute" version="1.6.0.0" targetFramework="net40" />
45
<package id="NUnit" version="2.6.2" targetFramework="net40" />
56
</packages>

NTestDataBuilder/NTestDataBuilder.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<Compile Include="DataBuilder.cs" />
4949
<Compile Include="DataBuilderExtensions.cs" />
5050
<Compile Include="Properties\AssemblyInfo.cs" />
51+
<Compile Include="ProxyBuilder.cs" />
5152
</ItemGroup>
5253
<ItemGroup>
5354
<None Include="NTestDataBuilder.nuspec" />

NTestDataBuilder/ProxyBuilder.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Reflection;
4+
using NSubstitute;
5+
6+
namespace NTestDataBuilder
7+
{
8+
/// <summary>
9+
/// Builds an NSubstitute proxy for the given type that has .Returns values set for the given dictionary of properties.
10+
/// </summary>
11+
/// <typeparam name="T">The type being proxied</typeparam>
12+
public class ProxyBuilder<T> where T : class
13+
{
14+
private readonly Dictionary<string, object> _properties;
15+
16+
/// <summary>
17+
/// Create a proxy builder to proxy the given property values for the type <see cref="T"/>.
18+
/// </summary>
19+
/// <param name="properties"></param>
20+
public ProxyBuilder(Dictionary<string, object> properties)
21+
{
22+
_properties = properties;
23+
}
24+
25+
/// <summary>
26+
/// Build the proxy object and set up the .Returns values for the properties.
27+
/// </summary>
28+
/// <returns>The proxy object</returns>
29+
public T Build()
30+
{
31+
var proxyEntity = Substitute.For<T>();
32+
var entityProperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
33+
foreach (var property in entityProperties.Where(property => _properties.ContainsKey(property.Name)))
34+
{
35+
property.GetValue(proxyEntity, null).Returns(_properties[property.Name]);
36+
}
37+
38+
return proxyEntity;
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)