You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+67-75Lines changed: 67 additions & 75 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -85,56 +85,53 @@ Prior to v2.0 this library was known as NTestDataBuilder.
85
85
86
86
3. Use the builder in a test, e.g.
87
87
88
-
var customer = new CustomerBuilder().WithFirstName("Robert").Build();
88
+
var customer = new CustomerBuilder()
89
+
.WithFirstName("Robert")
90
+
.Build();
89
91
90
92
4. Consider using the Object Mother pattern in combination with the builders, see [my blog post](http://robdmoore.id.au/blog/2013/05/26/test-data-generation-the-right-way-object-mother-test-data-builders-nsubstitute-nbuilder/) for a description of how I use this library.
91
93
92
-
How can I create a list of entities using my builders?
@@ -155,8 +152,8 @@ If you use the list builder functionality and get the following error:
155
152
156
153
Then you need to mark all the public methods on your builder as virtual. This is because we are using Castle Dynamic Proxy to generate lists and it can't intercept non-virtual methods.
157
154
158
-
Create Entities Implicitly
159
-
--------------------------
155
+
## Create Entities Implicitly
156
+
160
157
In the previous examples, you have seen how to create entities *explicitly*, by calling the `Build()` and `BuildList()` methods. For the ultimate in terseness, you can omit these methods, and Dossier will *implicitly* call them for you. The one caveat is that you must explicitly declare the variable type rather than using the `var` keyword (unless you are passing into a method with the desired type).
161
158
162
159
So, to create a single entity:
@@ -175,46 +172,48 @@ Or to create a list of entities:
175
172
List<Customer> data = CustomerBuilder.CreateListOfSize(3)
176
173
.TheFirst(1).WithFirstName("John");
177
174
178
-
Create object without requiring custom builder class
## Create object without requiring custom builder class
180
176
181
-
If you are building domain entities or other important classes having a custom builder class with intention-revealing method (e.g. WithFirstName) provides terseness (avoiding lambda expressions) and allows the builder class to start forming documentation about the usage of that object.
177
+
If you are building domain entities, or other important classes, having a custom builder class with intention-revealing method (e.g. WithFirstName) provides terseness (avoiding lambda expressions) and allows the builder class to start forming documentation about the usage of that object.
182
178
183
179
Sometimes though, you just want to build a class without that ceremony. Typically, we find that this applies for view models and DTOs.
184
180
185
-
In that instance you can use the generic Builder implementation as shown below:
181
+
In that instance you can use the generic `Builder` implementation as shown below:
182
+
183
+
StudentViewModel vm = Builder<StudentViewModel>.CreateNew()
184
+
.Set(x => x.FirstName, "Pi")
185
+
.Set(x => x.LastName, "Lanningham")
186
+
.Set(x => x.EnrollmentDate, new DateTime(2000, 1, 1));
187
+
188
+
var studentViewModels = Builder<StudentViewModel>.CreateListOfSize(5)
Note, that in the first example above, it was not necessary to call the `Build` method at the end of the method chain. This is because the `vm` variable has been defined as `StudentViewModel` and the C# compiler is able to infer the type and the object is set *implicitly*.
201
199
202
-
The syntax is modelled closely against what NBuilder provides and the behaviour of the class should be very similar.
200
+
In the second example, the `var` keyword is used to define `studentViewModels`, and so it is necessary to *explicitly* call `BuildList` to set the variable.
203
201
204
202
### Customising the construction of the object
205
203
206
-
By default the longest constructor of the class you specify will be called and then all properties (with public and private setters) will be set with values you specified (or anonymous values if none were specified).
207
-
208
-
Sometimes you might not want this behaviour, in which case you can specify a custom construction factory (see build objects without calling constructor section for explanation of factories) as shown below:
204
+
By default, the longest constructor of the class you specify will be called and then all properties (with public and private setters) will be set with values you specified (or anonymous values if none were specified).
Sometimes you might not want this behaviour, in which case you can specify a custom construction factory (see *Build objects without calling constructor* section for explanation of factories) as shown below:
var dtos = MixedAccessibilityDto dto = Builder<MixedAccessibilityDto>
213
+
.CreateListOfSize(5, new CallConstructorFactory())
214
+
.BuildList();
215
215
216
-
Build objects without calling constructor
217
-
-----------------------------------------
216
+
## Build objects without calling constructor
218
217
219
218
When you extend the `TestDataBuilder` as part of creating a custom builder you will be forced to override the abstract `BuildObject` method. You have full flexibility to call the constructor of your class directly as shown above, but you can also invoke some convention-based factories to speed up the creation of your builder (also shown above) using the `BuildUsing` method.
220
219
@@ -225,8 +224,7 @@ The `BuildUsing` method takes an instance of `IFactory`, of which you can create
225
224
*`CallConstructorFactory` - Calls the longest constructor with builder values (or anonymous values if none set) based on case-insensitive match of constructor parameter names against property names
226
225
*`AutoFixtureFactory` - Asks AutoFixture to create an anonymous instance of the class (note: does **not** use any builder values or anonymous values from Dossier)
227
226
228
-
Propogating the anonymous value fixture across builders
## Propagating the anonymous value fixture across builders
230
228
231
229
Within a particular instance of `AnonymousValueFixture`, which is created for every builder, any generators that return a sequence of values (e.g. unique values) will be maintained. If you want to ensure that the same anonymous value fixture is used across multiple related builders then:
232
230
@@ -258,13 +256,11 @@ Within a particular instance of `AnonymousValueFixture`, which is created for ev
258
256
259
257
There is currently no way to share an anonymous value fixture across unrelated builder instances. If this is something you need please raise an issue so we can discuss your requirement.
260
258
261
-
Anonymous Values and Equivalence Classes
262
-
----------------------------------------
259
+
## Anonymous Values and Equivalence Classes
263
260
264
261
todo: Coming soon!
265
262
266
-
How can I create proxy objects?
267
-
-------------------------------
263
+
## How can I create proxy objects?
268
264
269
265
This library integrates with [NSubstitute](http://nsubstitute.github.io/) for generating proxy objects, this means you can call the `AsProxy` method on your builder to request that the result from calling `Build` will be an NSubstitute proxy with the public properties set to return the values you have specified via your builder, e.g.
270
266
@@ -275,7 +271,6 @@ This library integrates with [NSubstitute](http://nsubstitute.github.io/) for ge
275
271
276
272
If you need to alter the proxy before calling `Build` to add complex behaviours that can't be expressed by the default public properties returns values then you can override the `AlterProxy` method in your builder, e.g.
277
273
278
-
```c#
279
274
class CustomerBuilder : TestDataBuilder<Customer, CustomerBuilder>
280
275
{
281
276
// ...
@@ -300,20 +295,17 @@ If you need to alter the proxy before calling `Build` to add complex behaviours
300
295
301
296
var customer = new CustomerBuilder().AsProxy().HasBeenMemberForYears(10);
302
297
var years = customer.CustomerForHowManyYears(DateTime.Now); // 10
303
-
```
304
298
305
299
*Remember that when using proxy objects of real classes that you need to mark properties and methods as virtual and have a protected empty constructor.*
306
300
307
-
Why does TestStack.Dossier have NSubstitute and AutoFixture as dependencies?
## Why does TestStack.Dossier have NSubstitute and AutoFixture as dependencies?
309
302
310
303
TestStack.Dossier is an opinionated framework and as such prescribes how to build your fixture data, including how to build lists, anonymous data and mock objects. Because of this we have decided to bundle it with the best of breed libraries for this purpose: AutoFixture and NSubstitute.
311
304
312
305
This allows for this library to provide a rich value-add on top of the basics of tracking properties in a dictionary in the `TestDataBuilder` base class. If you want to use different libraries or want a cut down version that doesn't come with NSubstitute or AutoFixture and the extra functionality they bring then take the `TestDataBuilder.cs` file and cut out the bits you don't want - open source ftw :).
313
306
314
307
If you have a suggestion for the library that can incorporate this value-add without bundling these libraries feel free to submit a pull request.
315
308
316
-
Contributions / Questions
317
-
-------------------------
309
+
## Contributions / Questions
318
310
319
311
If you would like to contribute to this project then feel free to communicate with Rob via Twitter (@robdmoore) or alternatively submit a pull request / issue.
0 commit comments