@@ -16,6 +16,7 @@ public abstract class TestDataBuilder<TObject, TBuilder>
1616 where TBuilder : TestDataBuilder < TObject , TBuilder > , new ( )
1717 {
1818 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 > > ( ) ;
1920 private ProxyBuilder < TObject > _proxyBuilder ;
2021
2122 /// <summary>
@@ -107,19 +108,32 @@ public TBuilder AsProxy()
107108 /// </summary>
108109 /// <param name="proxy">The proxy object</param>
109110 protected virtual void AlterProxy ( TObject proxy ) { }
110-
111+
111112 /// <summary>
112113 /// Records the given value for the given property from {TObject} and returns the builder to allow chaining.
113114 /// </summary>
114115 /// <typeparam name="TValue">The type of the property</typeparam>
115116 /// <param name="property">A lambda expression specifying the property to record a value for</param>
116- /// <param name="value">The builder so that other method calls can be chained</param>
117+ /// <param name="value">The value to set the property to</param>
118+ /// <returns>The builder so that other method calls can be chained</returns>
117119 public virtual TBuilder Set < TValue > ( Expression < Func < TObject , TValue > > property , TValue value )
118120 {
119121 _properties [ Reflector . GetPropertyNameFor ( property ) ] = value ;
120122 return this as TBuilder ;
121123 }
122124
125+ /// <summary>
126+ /// Records a given value provider for the given property from {TObject} and returns the builder to allow chaining.
127+ /// </summary>
128+ /// <typeparam name="TValue">The type of the property</typeparam>
129+ /// <param name="property">A lambda expression specifying the property to record a value for</param>
130+ /// <param name="factory">A method which produces instances of {TValue} for the property.</param>
131+ public virtual TBuilder Set < TValue > ( Expression < Func < TObject , TValue > > property , Func < TValue > factory )
132+ {
133+ _propFactories [ Reflector . GetPropertyNameFor ( property ) ] = ( ) => factory ( ) as object ;
134+ return this as TBuilder ;
135+ }
136+
123137 /// <summary>
124138 /// Gets the recorded value for the given property from {TObject} or an anonymous
125139 /// value if there isn't one specified.
@@ -129,10 +143,7 @@ public virtual TBuilder Set<TValue>(Expression<Func<TObject, TValue>> property,
129143 /// <returns>The recorded value of the property or an anonymous value for it</returns>
130144 public TValue Get < TValue > ( Expression < Func < TObject , TValue > > property )
131145 {
132- if ( ! Has ( property ) )
133- return Any . Get ( property ) ;
134-
135- return ( TValue ) _properties [ Reflector . GetPropertyNameFor ( property ) ] ;
146+ return ( TValue ) Get ( typeof ( TValue ) , Reflector . GetPropertyNameFor ( property ) ) ;
136147 }
137148
138149 /// <summary>
@@ -144,9 +155,13 @@ public TValue Get<TValue>(Expression<Func<TObject, TValue>> property)
144155 /// <returns></returns>
145156 public object Get ( Type type , string propertyName )
146157 {
147- if ( ! Has ( propertyName ) )
148- return Any . Get ( type , propertyName ) ;
149- return _properties [ propertyName ] ;
158+ object value ;
159+ if ( _properties . TryGetValue ( propertyName , out value ) ) return value ;
160+
161+ Func < object > factory ;
162+ if ( _propFactories . TryGetValue ( propertyName , out factory ) ) return factory ( ) ;
163+
164+ return Any . Get ( type , propertyName ) ;
150165 }
151166
152167 /// <summary>
@@ -193,7 +208,7 @@ protected bool Has<TValue>(Expression<Func<TObject, TValue>> property)
193208 /// <returns>Whether or not there is a recorded value for the property</returns>
194209 protected bool Has ( string propertyName )
195210 {
196- return _properties . ContainsKey ( propertyName ) ;
211+ return _properties . ContainsKey ( propertyName ) || _propFactories . ContainsKey ( propertyName ) ;
197212 }
198213
199214 /// <summary>
0 commit comments