16 Replies
Context 🙂
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?
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
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)
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)
Oh I mean constructor/object initializer my bad
Then it makes no difference. Setting a property from an object initialiser is identical to setting it explicitly after (barring
init
, required
, etc)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?
An object initialiser is literally just syntactic sugar for setting the properties one-by-one (again, ignoring
init
and required
). https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA+A3AhlABAMwEZcBeXAOxgHdcAxCCACgEpcBvXAQVN0IBpcAIR4AiAEwjcAXwDcAWABQi7HnxielGvSbN5CtQDpuZQnsPCy4kXsUABddsVtFuV7lsBmXAEtyAFy52XABzGD8ZXABnMIipFzdPd0IABiEg0PComOlFOKV8xSAYes, 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
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
Most of the properties are required, a typical initializer looks like:
Well, required properties can only be set from an initialiser, so question answered?
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?
Whatever is cleanest and most obvious to read
Okay thanks 😉 , really appreciate the help