Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoFixture always generate the same TimeZoneInfo #1097

Open
TrymBeast opened this issue Jan 16, 2019 · 8 comments
Open

AutoFixture always generate the same TimeZoneInfo #1097

TrymBeast opened this issue Jan 16, 2019 · 8 comments
Labels
bug triage Something that's being investigated
Milestone

Comments

@TrymBeast
Copy link

Hello,

I have found yesterday that AutoFixture always generates the same TimeZoneInfo, I had to implement one specimen builder to get a new one from the available TimeZoneInfos in the system.

Is this something that should be bundled in AutoFixture?

@zvirja
Copy link
Member

zvirja commented Jan 26, 2019

Hi,

This behavior happens because TimeZoneInfo doesn't have a public constructor. Therefore, AutoFixture tries to find first static method/property which returns TimeZoneInfo instance to support the Factory Method pattern. In our case it finds the TimeZoneInfo.Local property, which always returns the same time zone.

To get a random time zone for each time, add the following customization to your fixture:

fixture.Customizations.Add(new ElementsBuilder<TimeZoneInfo>(TimeZoneInfo.GetSystemTimeZones()));

That should help 😉

@Kralizek
Copy link
Contributor

Kralizek commented Jan 26, 2019

@zvirja do you think it would make sense to add registrations like this one to the base setup?

I have this one

fixture.Customizations.Add(new ElementsBuilder<Encoding>(Encoding.GetEncodings().Select(e => e.GetEncoding())));

If we look it from a mathematical point of view, we're telling AutoFixture what's the space of valid values of these types…

@TrymBeast
Copy link
Author

@zvirja I didn't knew about the ElementsBuilder, the only difference i see from my implementation is that the ElementsBuilder returns a random TimeZoneInfo while I return sequentially, but for the sake of tests it doesn't really matter. I will use the ElementsBuilder, thanks!

But my intention with this issue is more like what @Kralizek is asking, is this something that should already be configured in the base setup of AutoFixture?

@zvirja
Copy link
Member

zvirja commented Feb 3, 2019

@Kralizek @TrymBeast I don't mind adding this to the library, so it's supported natively. The only concern I have whether it might break the existing tests, as people might already rely on the TimeZoneInfo and Encoding values which are returned. Probably, it should be postponed till v5 to run it safely. @moodmosaic What do you think?

Additionally, we should consider implementing it in a way that it doesn't affect fixture construction performance. The call to TimeZoneInfo.GetSystemTimeZones() might be quite expensive. It would be especially a pity, if we never actually need to construct the TimeZoneInfo instances (as it usually happens). Likely, we should add some laziness around that - but should be investigated when it's implemented.

@Kralizek
Copy link
Contributor

Kralizek commented Feb 3, 2019

@zvirja Aren't registrations already evaluated only in case of need?

@moodmosaic
Copy link
Member

@zvirja, generated TimeZoneInfos shouldn't be always the same, so I'd label it as a bug and fix it in this version rather than waiting for vNext.

@zvirja zvirja added bug and removed question labels Mar 17, 2019
@zvirja
Copy link
Member

zvirja commented Mar 17, 2019

@moodmosaic Thanks for the feedback! Did as you suggested. Hope people don't rely on this behavior in their tests 🤞

@moodmosaic
Copy link
Member

don't rely on this behavior in their tests

I've rarely seen someone rely on a bug 😊

@aivascu aivascu added this to the v4.0 milestone Dec 5, 2023
@aivascu aivascu added the triage Something that's being investigated label Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug triage Something that's being investigated
Projects
None yet
Development

No branches or pull requests

5 participants