Data Data Builder Template: More Helpful or Better Service?

Let me start by saying that I am a huge fan of the elegance of this template - I have a group of basic images that I implemented for developers (especially for testing). However, I found (and this may be a caveat) that as my program developed, I had to go back and recycle the builders. In the end, it was really not worth keeping them up to date, and I returned, first of all, to save the mother base of objects, which have many pre-configured objects. Should I continue to update the collectors for future use, or is it a database, something that should be created only after you create the project, has reached some stability, and the object mask is getting too big?

Also note: I found that I am not using constructors elsewhere in the application, since I like to use the new .Net 3.0 syntax to initialize properties.

+4
source share
3 answers

I like to use free builders for the tested object to express the character of the object that I create. ObjectMothers tend to become bulky and tend (in the implementations I came across) to ultimately hide the details of creating objects.

For comparison:

User fred = CreateUser("fred").WithReputation(900) .WithScholarBadge() .WithCriticBadge() 

vs

 User fred = UserObjectMother.Fred() 

To express the idea that the user has a reputation of 900, and these two specific icons will be incompatible with ObjectMother. The trend I saw is the developers, who then find this method, which builds Fred() , which is close to what they need so that they add more attributes to the object. A free builder, on the other hand, is expressive of what is being built, and it is easy to create specific users for testing as needed.

However, I also use these patterns exclusively in test code, since production code usually does not require such expressiveness.

+8
source

If test data developers become too complicated to support, I would recommend switching to the mocking framework , which makes it more convenient for creating domain test objects with a known state.
In addition, with mocks, you can make test objects more than simple stubs and actually participate in what the test claims by setting expectations for how their properties and methods are called.

+1
source

Lambda syntax can make user-friendly interface more compact

For instance,

 User fred = new UserTestDataBuilder() .With(u => u.Name = "fred") .With(u => u.Reputation = 900) .With(u => u.ScholarBadge = true) .With(u => u.CriticBadge = true) 

You just need an Action method and a class to populate the properties.

 public class UserSpec { public string Name {get; set;} public int Reputation {get; set;} ... } public class UserTestDataBuilder() { private UserSpec _userSpec = new UserSpec(); public UserTestDataBuilder With(Action<UserSpec> action) { action(_userSpec); return this; } public User Build() { return new User(_userSpec.Name, _userSpec.Reputation, _userSpec.ScholarBadge, _userSpec.CriticBadge); } } 
0
source

Source: https://habr.com/ru/post/1277613/


All Articles