Timeline for Mocking / stubbing C# record types in a unit test
Current License: CC BY-SA 4.0
16 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 20, 2022 at 17:20 | comment | added | Filip Milovanović |
Sorry if you mentioned this somewhere, but, could you clarify: why is the metadataParser (or the metadata object in the second case) in the test? How does sut.Create() depend on these objects? Does it somehow call them internally (via some out-of-test wiring (or injection?)), does it indirectly depend on some result they produce, or what? E.g., what invokes metadataParser.Deserialize() ? And how is it that in one version, the SUT appears to depend on metadataParser , and on metadata in the other (is this just because you redesigned your classes in the second case)?
|
|
Aug 20, 2022 at 11:32 | comment | added | Ben Cottrell |
with expressions can be nested - stackoverflow.com/a/69449440/1301901
|
|
Aug 19, 2022 at 14:26 | comment | added | Robert Harvey |
If I were on the C# team, I would have considered providing a mutable option for records, as in mutable record MyRecord(...); .
|
|
Aug 19, 2022 at 13:56 | comment | added | void.pointer | @RobertHarvey I want to use record types, but I see a weakness in the language and much to be desired when I start using them in the context of unit tests. The fact that creating interfaces for everything is needed for unit tests also bothers me, but without them we spend a LOT more time in unit tests than we need to in opinion, and unit tests become a lot less useful when there's a crap ton of boilerplate I have to write, and start having to copy-pasta all over the place. | |
Aug 19, 2022 at 13:49 | history | edited | void.pointer | CC BY-SA 4.0 |
deleted 2 characters in body
|
Aug 19, 2022 at 13:44 | answer | added | JonasH | timeline score: 2 | |
Aug 19, 2022 at 13:12 | comment | added | Caleth |
It's this. That answer defines the class Lens
|
|
Aug 19, 2022 at 13:11 | comment | added | void.pointer |
@Caleth Thanks; the link I posted talks about Lenses, but I don't see native support for it. So I don't know what you are referring to with Lens . And with is something I'd like to use, but I don't think there's a syntax for it to modify properties of nested records. e.g. something like metadata with { JsonPaths.Radarr.CustomFormats = new[] {} }
|
|
Aug 19, 2022 at 13:06 | comment | added | void.pointer | @RobertHarvey The interfaces idea is one possible solution. But what about others? That's why I'm posting here. To discover more solutions and more tradeoffs and make an educated decision on which approach is better. It's possible that I end up changing nothing. Your comment isn't relevant because it doesn't directly respond to my question; I am not measuring relevance in terms of software development at large. | |
Aug 19, 2022 at 13:00 | comment | added | Caleth |
The examples in that link seem like they would be improved with record s and with expressions, especially Lens es.
|
|
Aug 19, 2022 at 12:53 | comment | added | Robert Harvey | It actually is relevant. Software development is an exercise in tradeoffs; will it take more time to maintain your unit tests or to maintain those interfaces? | |
Aug 19, 2022 at 12:51 | comment | added | void.pointer | @Caleth Yes, it can, but then how do I modify the properties I care about? There's no easy way to do that unless I make the properties mutable. There are workarounds, like those documented in the link posted below. But I wasn't particularly happy with those solutions. stackoverflow.com/questions/30938972/… | |
Aug 19, 2022 at 12:50 | comment | added | void.pointer | @RobertHarvey Sure, two minutes today. But what if I have dozens more of these tests? What if the object grows later and two minutes becomes 30 minutes? But beside all of that, your point is not relevant to my question. | |
Aug 19, 2022 at 12:46 | comment | added | Caleth | Is AutoFixture not able to generate records directly? | |
Aug 19, 2022 at 12:38 | comment | added | Robert Harvey | It takes two minutes to fix up your unit tests so that they function again. | |
Aug 19, 2022 at 12:35 | history | asked | void.pointer | CC BY-SA 4.0 |