Skip to content

Commit 6eaee0b

Browse files
Fixes up the set lambda, the function pattern now underlies other methods
- It's a lot cleaner to use the factory lambdas as just constant functions for the other Set method.
1 parent bf636b8 commit 6eaee0b

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

TestStack.Dossier.Tests/ProxyBuilderTests.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ProxyBuilderTests
1212
[Fact]
1313
public void GivenClassToProxyWithNoProperties_WhenBuildingProxy_ReturnAClassWithNoReturnsValuesSet()
1414
{
15-
var proxyBuilder = new ProxyBuilder<Customer>(new Dictionary<string, object>());
15+
var proxyBuilder = new ProxyBuilder<Customer>(new Dictionary<string, Func<object>>());
1616

1717
var proxy = proxyBuilder.Build();
1818

@@ -24,33 +24,34 @@ public void GivenClassToProxyWithNoProperties_WhenBuildingProxy_ReturnAClassWith
2424
[Fact]
2525
public void GivenClassToProxyWithNoProperties_WhenBuildingProxy_ReturnAnNSubstituteProxyOfThatClass()
2626
{
27-
var proxyBuilder = new ProxyBuilder<Customer>(new Dictionary<string, object>());
27+
var proxyBuilder = new ProxyBuilder<Customer>(new Dictionary<string, Func<object>>());
2828

2929
var proxy = proxyBuilder.Build();
3030

3131
proxy.DidNotReceive().CustomerForHowManyYears(Arg.Any<DateTime>());
3232
}
3333

3434
[Fact]
35-
public void GivenClassToProxyWithSinglePropertyValue_WhenBuildingProxy_ReturnAClassWithReturnValueSet()
35+
public void GivenClassToProxyWithSinglePropertyValue_WhenBuildingProxy_ReturnAClassWithReturnValueSetFromFunction()
3636
{
37-
var proxyBuilder = new ProxyBuilder<Customer>(new Dictionary<string, object> {{"FirstName", "FirstName"}});
37+
int nonce = new Random().Next(0, 100);
38+
var proxyBuilder = new ProxyBuilder<Customer>(new Dictionary<string, Func<object>> {{"FirstName", () => "FirstName" + nonce}});
3839

3940
var proxy = proxyBuilder.Build();
4041

41-
proxy.FirstName.ShouldBe("FirstName");
42+
proxy.FirstName.ShouldBe("FirstName" + nonce);
4243
proxy.LastName.ShouldBe(string.Empty);
4344
proxy.YearJoined.ShouldBe(0);
4445
}
4546

4647
[Fact]
4748
public void GivenClassToProxyWithMultiplePropertyValues_WhenBuildingProxy_ReturnAClassWithReturnValueSet()
4849
{
49-
var proxyBuilder = new ProxyBuilder<Customer>(new Dictionary<string, object>
50+
var proxyBuilder = new ProxyBuilder<Customer>(new Dictionary<string, Func<object>>
5051
{
51-
{ "FirstName", "FirstName" },
52-
{ "LastName", "LastName" },
53-
{ "YearJoined", 1 },
52+
{ "FirstName", () => "FirstName" },
53+
{ "LastName", () => "LastName" },
54+
{ "YearJoined", () => 1 },
5455
}
5556
);
5657

@@ -64,10 +65,10 @@ public void GivenClassToProxyWithMultiplePropertyValues_WhenBuildingProxy_Return
6465
[Fact]
6566
public void GivenClassWithSomeVirtualProperties_WhenBuildingProxy_ThenOnlyVirtualMembersAreProxied()
6667
{
67-
var proxyBuilder = new ProxyBuilder<Company>(new Dictionary<string, object>()
68+
var proxyBuilder = new ProxyBuilder<Company>(new Dictionary<string, Func<object>>()
6869
{
69-
{"Name", "Vandelay Industries"},
70-
{"EmployeeCount", 100}
70+
{"Name", () => "Vandelay Industries"},
71+
{"EmployeeCount", () => 100}
7172
});
7273

7374
var proxy = proxyBuilder.Build();

TestStack.Dossier/ProxyBuilder.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Reflection;
45
using NSubstitute;
@@ -11,13 +12,13 @@ namespace TestStack.Dossier
1112
/// <typeparam name="T">The type being proxied</typeparam>
1213
public class ProxyBuilder<T> where T : class
1314
{
14-
private readonly Dictionary<string, object> _properties;
15+
private readonly Dictionary<string, Func<object>> _properties;
1516

1617
/// <summary>
1718
/// Create a proxy builder to proxy the given property values for the type {T}.
1819
/// </summary>
1920
/// <param name="properties"></param>
20-
public ProxyBuilder(Dictionary<string, object> properties)
21+
public ProxyBuilder(Dictionary<string, Func<object>> properties)
2122
{
2223
_properties = properties;
2324
}
@@ -33,7 +34,7 @@ public T Build()
3334
foreach (var property in properties.Where(property => _properties.ContainsKey(property.Name)))
3435
{
3536
if (property.GetGetMethod().IsVirtual)
36-
property.GetValue(proxy, null).Returns(_properties[property.Name]);
37+
property.GetValue(proxy, null).Returns(_properties[property.Name]());
3738
}
3839

3940
return proxy;

TestStack.Dossier/TestDataBuilder.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ public abstract class TestDataBuilder<TObject, TBuilder>
1515
where TObject : class
1616
where TBuilder : TestDataBuilder<TObject, TBuilder>, new()
1717
{
18-
private readonly Dictionary<string, object> _properties = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
19-
private readonly Dictionary<string, Func<object>> _propFactories = new Dictionary<string, Func<object>>();
18+
private readonly Dictionary<string, Func<object>> _properties = new Dictionary<string, Func<object>>();
2019
private ProxyBuilder<TObject> _proxyBuilder;
2120

2221
/// <summary>
@@ -118,7 +117,7 @@ protected virtual void AlterProxy(TObject proxy) {}
118117
/// <returns>The builder so that other method calls can be chained</returns>
119118
public virtual TBuilder Set<TValue>(Expression<Func<TObject, TValue>> property, TValue value)
120119
{
121-
_properties[Reflector.GetPropertyNameFor(property)] = value;
120+
_properties[Reflector.GetPropertyNameFor(property)] = () => value;
122121
return this as TBuilder;
123122
}
124123

@@ -130,7 +129,7 @@ public virtual TBuilder Set<TValue>(Expression<Func<TObject, TValue>> property,
130129
/// <param name="factory">A method which produces instances of {TValue} for the property.</param>
131130
public virtual TBuilder Set<TValue>(Expression<Func<TObject, TValue>> property, Func<TValue> factory)
132131
{
133-
_propFactories[Reflector.GetPropertyNameFor(property)] = () => factory() as object;
132+
_properties[Reflector.GetPropertyNameFor(property)] = () => factory() as object;
134133
return this as TBuilder;
135134
}
136135

@@ -155,11 +154,8 @@ public TValue Get<TValue>(Expression<Func<TObject, TValue>> property)
155154
/// <returns></returns>
156155
public object Get(Type type, string propertyName)
157156
{
158-
object value;
159-
if (_properties.TryGetValue(propertyName, out value)) return value;
160-
161157
Func<object> factory;
162-
if (_propFactories.TryGetValue(propertyName, out factory)) return factory();
158+
if (_properties.TryGetValue(propertyName, out factory)) return factory();
163159

164160
return Any.Get(type, propertyName);
165161
}
@@ -208,7 +204,7 @@ protected bool Has<TValue>(Expression<Func<TObject, TValue>> property)
208204
/// <returns>Whether or not there is a recorded value for the property</returns>
209205
protected bool Has(string propertyName)
210206
{
211-
return _properties.ContainsKey(propertyName) || _propFactories.ContainsKey(propertyName);
207+
return _properties.ContainsKey(propertyName);
212208
}
213209

214210
/// <summary>

0 commit comments

Comments
 (0)