C
C#•3mo ago
Jiry_XD

Test setters indirectly via constructor or test them directly?

Title 🙂
16 Replies
canton7
canton7•3mo ago
Context 🙂
Jiry_XD
Jiry_XDOP•3mo ago
I am testing a customer class, which has some properties such as CompanyName this setter just checks for null and empty, ... But some setters such as PhoneNumber or VatNumber check if they are valid through regex. Do I test these setters of the properties through the constructor (indirectly) or test them directly, making a seperate test method for each?
canton7
canton7•3mo ago
I'd test them explicitly. If the class was later refactored so that validation was done in the ctor but not the setter, then you'd want the tests to start failing
Jiry_XD
Jiry_XDOP•3mo ago
Gotcha I get you in that sense. But I am not using a constructor 😬 , using object initializers. In the properties we either use Guard.Against.NullOrEmpty(value) or Guard.Against.ValidVatNumber(value)
canton7
canton7•3mo ago
So how would you go about testing the properties via the constructor, if they're not set by the constructor? This is where actually asking a detailed question, which includes relevant code, is useful (instead of just a single short sentence)
Jiry_XD
Jiry_XDOP•3mo ago
Oh I mean constructor/object initializer my bad
canton7
canton7•3mo ago
Then it makes no difference. Setting a property from an object initialiser is identical to setting it explicitly after (barring init, required, etc)
Jiry_XD
Jiry_XDOP•3mo ago
Gotcha, so I can choose whether I do it in a test with the whole initializer or seperate setters? What would you do? Certainly one approach would be cleaner than the other no?
Jiry_XD
Jiry_XDOP•3mo ago
Yes, thanks, object is firstly made with constructor then after all properties setters are being called But still the question remains what is cleaner, testing setters seperatly or the using the initializer? I am not sure what would you do? I think it will become cluttered testing through initializer cause I do have some required props
canton7
canton7•3mo ago
It depends on how the objects are normally constructed, what the tests look like, whether any properties are required/init-only, etc It's pretty much the same question as to whether to initialise any old object using an object initialiser or by manually setting the properties one-by-one It's a judgement call. If you give me two code snippets I can say which I find easiest to read, but it's hard to say anything more specific
Jiry_XD
Jiry_XDOP•3mo ago
Most of the properties are required, a typical initializer looks like:
new() {
ShippingAddress=addresses[0],
BillingAddress=addresses[1],
CompanyName="Tech Corp",
ContactPerson="John Doe",
VatNumber="FR0817544902",
EmailAddress=new("[email protected]"),
PhoneNumber="+33 123456789"
},
new() {
ShippingAddress=addresses[0],
BillingAddress=addresses[1],
CompanyName="Tech Corp",
ContactPerson="John Doe",
VatNumber="FR0817544902",
EmailAddress=new("[email protected]"),
PhoneNumber="+33 123456789"
},
canton7
canton7•3mo ago
Well, required properties can only be set from an initialiser, so question answered?
Jiry_XD
Jiry_XDOP•3mo ago
Oh True damn haha Thanks But I still could test them after initialization Thanks for the insights What I could do is do a constructor for the test class that does the setup for the tests and then test the setters individually, I think it will look a lot cleaner if you look above the initializer is quite large, what do you think?
canton7
canton7•3mo ago
Whatever is cleanest and most obvious to read
Jiry_XD
Jiry_XDOP•3mo ago
Okay thanks 😉 , really appreciate the help
Want results from more Discord servers?
Add your server