I really like using this approach in my own tests, actually
@gui.ferreira4 ай бұрын
💪
@Laggie749 ай бұрын
I can see that the Directors might be needed in more complex situations. But instead of creating multiple Directors for each pizza type, wouldn't it be simpler to just add a fluent method "StartWith(PizzaBuilder.PizzaType.Plain)" that takes in a class specific enum? I know this would place the responsibility of the default configuration as part of the Builder, but it would make sense here to simplify code maintenance and readability. You can still create the directors later if you want that control somewhere else. Thanks for all the great content.
@gui.ferreira9 ай бұрын
Thanks! That simple approach will often work just fine. The problem might be once you have multiple recipes where you keep their description. Directors let you segregate that. Thanks for the comment
@lunat38 Жыл бұрын
That is an ideal use case for Builders, well done! And thanks for sharing.
@gui.ferreira Жыл бұрын
You bet! Thanks 🙏
@BenvdStouwe Жыл бұрын
After years of using the Builder Design Pattern to setup testdata I have found a better solutoin: C# records. I create a static method that instantiates a "default" instance of a record and with the "with" operator I setup further properties. This way I don't have to create Builder classes with all their properties and methods while I maintain the same functionality.
@gui.ferreira Жыл бұрын
That's a good idea. I think it doesn't cover all the scenarios where I use Builders, however, I can see myself adopting it. Thanks for sharing!
@emiraksoy15202 ай бұрын
Awesome ! Definitely I will try this on unit tests , can we apply this integration tests ?
@gui.ferreira2 ай бұрын
Yes, you can. If you have repetitive data setup, the builder pattern can be magic.
@fandermill Жыл бұрын
Thank you for this great content, Gui! Do you think it is ok to set private fields with Reflection in the Build method to get the object in the needed state for the test? Or do you prefer an internal constructor/factory method that can be called trough the InternalsVisibleTo option? Or is there any other option?
@gui.ferreira Жыл бұрын
You can do it. However, I avoid using techniques like reflection on my tests. On my tests, I avoid going around the public API. I want to feel the pain of consumers. So, if I feel the need to go around the public interface, I question my design first.
@bmassioui Жыл бұрын
I prefer to go with factory method with private constructor ...
@gui.ferreira Жыл бұрын
Builders are extremely useful when you have types involved in multiple Test Classes.
@stefan-d.grigorescu11 ай бұрын
@@gui.ferreiraBut what is the difference between the Director and a Factory? In the end, both of them abstract the construction of an object, returning a specific default configuration (that could be further configured, depending on the type being created, if it exposes setters or not)
@gui.ferreira10 ай бұрын
@@stefan-d.grigorescu the Director is also a creational pattern, but it's deeply connected to the Builder Pattern. You will use those two instead of a Factory when you have a complex initialization.