We are using NBuilder for generating test data for our tests. We have a few models with private setters. So setting thos properties is only possible throughout the constructor. The problem is that we get the following error from NBuilder:
CompanyAddressViewModel does not have a default parameterless constructor
I understand the error. Do I really need to create a default constructor in my class?
Here is my NBuilder code:
Builder<CompanyAddressViewModel>.CreateNew().Build()
You can use WithConstructor() or WithFactory() method, in Net Core 2.1 WithConstructor() method is deprecated.
Builder<CompanyAddressViewModel>.CreateNew(). WithFactory (() => new parameter()).Build()
Former versions uses WithConstructor() method, but it's obsolete in these days, so you should use WithFactory() method:
Builder<CompanyAddressViewModel>
.CreateNew()
.WithFactory(() => new CompanyAddressViewModel("your", "parameter"))
.Build()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With