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
Creating MailMessage doesn't work #490
Comments
It's true that AutoFixture can't create It's quite trivial to make it: var actual = fixture.Build<MailMessage>().OmitAutoProperties().Create(); or, if you want a Customization: fixture.Customize<MailMessage>(c => c.OmitAutoProperties()); Granted, in both cases, the Should AutoFixture be able to create I'm torn: AdvantagesAutoFixture aims for the pit of success. I honestly have my doubts whether we've succeeded being there. Mostly, I think I've failed in pulling AutoFixture in that direction, but AutoFixture still works fairly well in many cases. It's certainly not unsuccessful... Still, according to that goal, it's important that it just works, which it patently doesn't for DisadvantagesThe .NET Base Class Library (BCL) has thousands of types. It was never my goal that AutoFixture should be able to create all of them. For one, it'd be a Sisyphean task; also, it'd be an arms race, because new types are added with each new release of .NET. Instead, I always envisioned giving AutoFixture a good set of heuristics for figuring out how to create objects and other values (I'll get back to this later), knowing full well that it wouldn't address 100% of all types. To that, I added specific strategies for well-known primitives, such as Over time, we've added a few more of those. My major problem with adding explicit support for One of the issues (perhaps the most important one) we have for AutoFixture 4 is #404 (Support for CoreCLR). I don't know if We already have enough features the we need to filter out in one way or another in order to be able to make AutoFixture run on CoreCLR, but I'm not sure adding more is a wise decision at the moment... New heuristicsIf it's only All three are properties of the type What's special about this abstract class, though, is that it has static properties of its own type; e.g.
AutoFixture already has Would it make sense to add a heuristic to AutoFixture that does the same for static properties and fields? Could it break existing code? I'm honestly not sure about the 'right' direction to take here, so I'd like to solicit opinions on the matter, particularly, but not exclusively, from the AutoFixture core members @adamchester, @ecampidoglio, and @moodmosaic. |
BTW, @tiesmaster, thank you for bringing this up 👍 As you can see, it's an interesting enough question that I got a little carried away 😳 |
IMHO, it's not hard to create Granted, it takes an additional call to the And while this results to an empty object:
This results to a filled one:
On a side note: The System.Net.Mail.MailMessage seems to defined in the System.dll which looks like it is part of the PCL, though it doesn't seem to show up in the CoreCRL API reference page. Based on @ploeh's comment, and the above, I probably wouldn't add a built-in generator for this. It is definitely an interesting question though, so thank you @tiesmaster for asking 👍 |
@ploeh and @moodmosaic Thanks for the elaborate discussion, and I agree it's a difficult call to make, especially with CoreCLR coming up. Having the creation of However, the lengthy post also resulted in some more reflection on my side, and I realized that this isn’t really about Next, it also triggered me to ask "why did I stumble upon this?", and "why did I raise this issue?". The problem was that the object creation failed, and the exception message, and stack trace was rather poor. Although, it was clear that it wasn't able to create this type of object, but not why, and how to solve this. So I tried to find out why it was failing, found out that it was because of properties with type of Regarding factory properties and fields, I do like that approach. However, I'd expect that it might lead to some really modified behavior for AutoFixture, in some situations. So that wouldn't be my first approach, if we still want to fix this. |
Given that in most (if not all) scenarios the Fixture instance is customized, what do you mean by that? |
@ploeh Playing around a little bit with this, I found out that So the need for a "
@moodmosaic I don't know exactly what you mean by "[...] most (if not all) scenarios the Fixture instance is customized", since in a lot of situations for me at work, the Fixture instance is not customized. However, I don't think this is still really relevant, because, as indicated above, AutoFixture already does that ;) |
It looks like the problem is that a property or field setter (Encoding) throws an exception. Wouldn't the best solution be to have a much better exception message when this happens? |
I would not be in favour of adding a default configuration for MailMessage or Encoding or any similar types, only because I feel like it wouldn't be so common to create instances of these types for tests. I'm not basing this on any actual data; it's just a gut feeling. Also, as the user of AutoFixture, trying to create robust tests, shouldn't you be aware of skipped properties in instances you are using? How do we make you aware the we automatically skipped some properties of MailMessage (or similar) instances? |
@tiesmaster, thank you for challenging my assumptions. Digging a little further, it turns out that creation fails when AutoFixture attempts to populate an Although I agree with @adamchester that a better exception message wouldn't hurt, the information is actually already there. If you read the entire exception message, you'll see this:
What we could consider would be to add a feature to I'm not saying that this will allow AutoFixture to create instances of |
Always looking for an I think including the For example, since the property setter threw an exception, we can explain the few options available: customise with a factory, ignore/without, provide a default value, etc? Regards,
|
I actually encountered the same issue a few months ago, and I solved it the simplest way possible by regitering the
The behaviour described previously is what I understood at the time. The fundamental issue is that Would it be a sin to consider adding exceptions for well known types of the BCL that pose problems? The choice of the returned encoding in this case would be arbitrary, of course, but I think average users would be happier if things just worked for BCL types. |
It would not be the end of the world to add exceptions for common BCL types. The problem is the magic behind it remains hidden, I'm not sure what the right approach is here; needs a @ploeh decision:) |
I just tried to create a System.Net.IPAddress and as I feared, this also throws an exception:
This is due to the Admittedly, IPAddress has design flaws, but I think it would be nice if AutoFixture knew about those well known flawed types and handled them in an exceptional manner. |
I think, it wouldn't be easy to support each and every poorly designed built-in BCL object, because at the end AutoFixture can't really control the direction of the BCL... IMHO, the easiest way to deal with all these, is by customizing the This technique, of using custom functions/instances/objects, also exists in QuickCheck, Quviq QuickCheck for Erlang, FsCheck, and probably others – AFAIK, none of these tools support each and every base data structure OOTB... |
I'm not going to hide the fact that I'm torn on the general issue. On the one hand, I think it would be a benefit to AutoFixture if, in general, it just works. The more it just works, the better. On the other hand, I'm concerned that AutoFixture is going to grow. The code base is big enough already. It's important to understand, though, that the value in any piece of software is not in how easy or difficult it is to develop and maintain, but how easy it is to use. This makes me lean towards adding explicit support for As a principle, I don't think AutoFixture should attempt to cover all BCL classes. If, on the other hand, there's a user of AutoFixture who needs to use a particular BCL class, and wants to put in the effort of writing said support into AutoFixture, I think we should accept the contribution. In other words: feel free to send a pull request for |
👍 |
I believe the I'm happy to take this, unless @tiesmaster wants to do it 😄 However I'll probably ask for some guidances when it comes to design choices as I'm not so familiar with all the inner mechanics. |
Feel free to ask as much as you'd like 👍 😄 |
@Pvlerick I'd like to pick this up myself, though, I will be leaving to Iceland in a couple of days, so I might not be able to complete this before that. If you're really in a hurry with this, then go ahead and pick this up, otherwise I'd like to complete this ;) |
@tiesmaster No I'm not in a hurry at all, knock yourself out 😄 |
Similar to #441, creating a
MailMessage
doesn't work without customizing an instance of AutoFixture, as shown by this test:Which throws
TargetInvocationException
.This is because the encoding properties (
BodyEncoding
,HeadersEncoding
, andSubjectEncoding
) require anSystem.Text.Encoding
instance, and it appears that AutoFixture is not able to create those as well.Injecting something like
Encoding.UTF8
solves the problem. Maybe the same approach could be taken like #427, by adding aUtf8EncodingGenerator
.The text was updated successfully, but these errors were encountered: