|
1 |
| -NTestDataBuilder |
2 |
| -================ |
| 1 | +# Dossier |
3 | 2 |
|
4 |
| -NTestDataBuilder provides you with the code infrastructure to easily and quickly generate test fixture data for your automated tests in a terse, readable and maintainable way using the Test Data Builder pattern. |
| 3 | +Dossier provides you with the code infrastructure to easily and quickly generate test fixture data for your automated tests in a terse, readable and maintainable way using the Test Data Builder pattern. |
5 | 4 |
|
6 | 5 | For more information please see the [blog post](http://robdmoore.id.au/blog/2013/05/26/test-data-generation-the-right-way-object-mother-test-data-builders-nsubstitute-nbuilder/) that gives the theory behind the approach this library was intended for and the [presentation and example code](https://github.com/robdmoore/TestFixtureDataGenerationPresentation) that gives a concrete example of the usage of the library (and the theory behind it).
|
7 | 6 |
|
8 |
| -NTestDataBuilder is integrated with NSubstitute for proxy/mock/substitute object generation and AutoFixture for anonymous value generation. Version 1 was integrated with NBuilder for list generation, but that is now replaced with internal code that uses Castle Dynamic Proxy (il-merged into the dll) for an even terser syntax. |
| 7 | +Dossier is integrated with NSubstitute for proxy/mock/substitute object generation and AutoFixture for anonymous value generation. Version 1 was integrated with NBuilder for list generation, but that is now replaced with internal code that uses Castle Dynamic Proxy (il-merged into the dll) for an even terser syntax. |
9 | 8 |
|
10 |
| -How do I get started? |
11 |
| ---------------------- |
| 9 | +## How do I get started? |
12 | 10 |
|
13 |
| -1. `Install-Package NTestDataBuilder` |
14 |
| -2. Create a builder class for one of your objects, e.g. if you have a customer: |
15 |
| -```c# |
16 |
| - // Customer.cs |
17 |
| - |
18 |
| - public class Customer |
19 |
| - { |
20 |
| - protected Customer() {} |
| 11 | +1. `Install-Package TestStack.Dossier` |
21 | 12 |
|
22 |
| - public Customer(string firstName, string lastName, int yearJoined) |
23 |
| - { |
24 |
| - if (string.IsNullOrEmpty(firstName)) |
25 |
| - throw new ArgumentNullException("firstName"); |
26 |
| - if (string.IsNullOrEmpty(lastName)) |
27 |
| - throw new ArgumentNullException("lastName"); |
28 |
| - |
29 |
| - FirstName = firstName; |
30 |
| - LastName = lastName; |
31 |
| - YearJoined = yearJoined; |
32 |
| - } |
33 |
| - |
34 |
| - public virtual int CustomerForHowManyYears(DateTime since) |
35 |
| - { |
36 |
| - if (since.Year < YearJoined) |
37 |
| - throw new ArgumentException("Date must be on year or after year that customer joined.", "since"); |
38 |
| - return since.Year - YearJoined; |
39 |
| - } |
| 13 | +2. Create a builder class for one of your objects, e.g. if you have a customer: |
40 | 14 |
|
41 |
| - public virtual string FirstName { get; private set; } |
42 |
| - public virtual string LastName { get; private set; } |
43 |
| - public virtual int YearJoined { get; private set; } |
44 |
| - } |
45 |
| - |
46 |
| - // CustomerBuilder.cs |
47 |
| - |
48 |
| - public class CustomerBuilder : TestDataBuilder<Customer, CustomerBuilder> |
49 |
| - { |
50 |
| - public CustomerBuilder() |
51 |
| - { |
52 |
| - // Can set up defaults here - any that you don't set will have an anonymous value generated by default. |
53 |
| - WhoJoinedIn(2013); |
54 |
| - } |
| 15 | + // Customer.cs |
| 16 | + |
| 17 | + public class Customer |
| 18 | + { |
| 19 | + protected Customer() {} |
| 20 | + |
| 21 | + public Customer(string firstName, string lastName, int yearJoined) |
| 22 | + { |
| 23 | + if (string.IsNullOrEmpty(firstName)) |
| 24 | + throw new ArgumentNullException("firstName"); |
| 25 | + if (string.IsNullOrEmpty(lastName)) |
| 26 | + throw new ArgumentNullException("lastName"); |
| 27 | + |
| 28 | + FirstName = firstName; |
| 29 | + LastName = lastName; |
| 30 | + YearJoined = yearJoined; |
| 31 | + } |
| 32 | + |
| 33 | + public virtual int CustomerForHowManyYears(DateTime since) |
| 34 | + { |
| 35 | + if (since.Year < YearJoined) |
| 36 | + throw new ArgumentException("Date must be on year or after year that customer joined.", "since"); |
| 37 | + return since.Year - YearJoined; |
| 38 | + } |
| 39 | + |
| 40 | + public virtual string FirstName { get; private set; } |
| 41 | + public virtual string LastName { get; private set; } |
| 42 | + public virtual int YearJoined { get; private set; } |
| 43 | + } |
| 44 | + |
| 45 | + // CustomerBuilder.cs |
| 46 | + |
| 47 | + public class CustomerBuilder : TestDataBuilder<Customer, CustomerBuilder> |
| 48 | + { |
| 49 | + public CustomerBuilder() |
| 50 | + { |
| 51 | + // Can set up defaults here - any that you don't set will have an anonymous value generated by default. |
| 52 | + WhoJoinedIn(2013); |
| 53 | + } |
| 54 | + |
| 55 | + public CustomerBuilder WithFirstName(string firstName) |
| 56 | + { |
| 57 | + return Set(x => x.FirstName, firstName); |
| 58 | + } |
| 59 | + |
| 60 | + public CustomerBuilder WithLastName(string lastName) |
| 61 | + { |
| 62 | + return Set(x => x.LastName, lastName); |
| 63 | + } |
| 64 | + |
| 65 | + public CustomerBuilder WhoJoinedIn(int yearJoined) |
| 66 | + { |
| 67 | + return Set(x => x.YearJoined, yearJoined); |
| 68 | + } |
| 69 | + |
| 70 | + protected override Customer BuildObject() |
| 71 | + { |
| 72 | + return new Customer( |
| 73 | + Get(x => x.FirstName), |
| 74 | + Get(x => x.LastName), |
| 75 | + Get(x => x.YearJoined) |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
55 | 79 |
|
56 |
| - public CustomerBuilder WithFirstName(string firstName) |
57 |
| - { |
58 |
| - return Set(x => x.FirstName, firstName); |
59 |
| - } |
60 | 80 |
|
61 |
| - public CustomerBuilder WithLastName(string lastName) |
62 |
| - { |
63 |
| - return Set(x => x.LastName, lastName); |
64 |
| - } |
| 81 | +3. Use the builder in a test, e.g. |
65 | 82 |
|
66 |
| - public CustomerBuilder WhoJoinedIn(int yearJoined) |
67 |
| - { |
68 |
| - return Set(x => x.YearJoined, yearJoined); |
69 |
| - } |
| 83 | + var customer = new CustomerBuilder().WithFirstName("Robert").Build(); |
70 | 84 |
|
71 |
| - protected override Customer BuildObject() |
72 |
| - { |
73 |
| - return new Customer( |
74 |
| - Get(x => x.FirstName), |
75 |
| - Get(x => x.LastName), |
76 |
| - Get(x => x.YearJoined) |
77 |
| - ); |
78 |
| - } |
79 |
| - } |
80 |
| -``` |
81 |
| -3. Use the builder in a test, e.g. |
82 |
| -```c# |
83 |
| - var customer = new CustomerBuilder().WithFirstName("Robert").Build(); |
84 |
| -``` |
85 | 85 | 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.
|
86 | 86 |
|
87 | 87 | How can I create a list of entities using my builders?
|
@@ -187,10 +187,10 @@ If you need to alter the proxy before calling `Build` to add complex behaviours
|
187 | 187 |
|
188 | 188 | *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.*
|
189 | 189 |
|
190 |
| -Why does NTestDataBuilder have NSubstitute and AutoFixture as dependencies? |
| 190 | +Why does Dossier have NSubstitute and AutoFixture as dependencies? |
191 | 191 | ------------------------------------------------------------------------
|
192 | 192 |
|
193 |
| -NTestDataBuilder 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. |
| 193 | +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. |
194 | 194 |
|
195 | 195 | 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 :).
|
196 | 196 |
|
|
0 commit comments