0

I have added MSTests to my C# WinForms project. To test if they are working right, I wanted to test the following function:

public static bool IsValidEmail(string email)
{
    return new EmailAddressAttribute().IsValid(email);
}

To do this I have written these two tests:

[TestMethod]
public void Test_IsValidEmail_True()
{
    Assert.IsTrue(IsValidEmail("test@test.pl"),"Good mail test");
}
[TestMethod]
public void Test_IsValidEmail_False()
{
    Assert.IsFalse(IsValidEmail("test@test"), "Bad mail test");
}

The function works correctly and checks if the email is correct.

That means that the funtcion IsValidEmail("test@test") returns false. However the second test passes only if I input it with for example "test", but if I input test@test it will not pass and outputs:

Message: Assert.IsFalse failed. Bad mail test

Why is this test failing?

Here is how I tested the function if it works manually:

MessageBox.Show("test@test.pl - " + IsValidEmail("test@test.pl"));
MessageBox.Show("test@test - " + IsValidEmail("test@test"));

test 1 test 2

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
TK-421
  • 294
  • 1
  • 7
  • 26
  • Clearly `IsValidEmail("test@test")` is returning true, so the actual question should be "Why does `EmailAddressAttribute.IsValid("test@test")` return true?" – Matthew Watson Feb 13 '20 at 09:40
  • @MatthewWatson but I wrote in my question that the function works correctly and if I input "test@test" in a running application, it shows it as incorrect, so, my question is correct. – TK-421 Feb 13 '20 at 09:42
  • When I try `Console.WriteLine(new EmailAddressAttribute().IsValid("test@test"));` it prints `true`, so I cannot reproduce your result. – Matthew Watson Feb 13 '20 at 09:43
  • I tested `"test@test"` in .Net Core 3.1 and it's `True`. Also why do you unit test other's library? – Louis Go Feb 13 '20 at 09:49
  • @MatthewWatson I have added proof in my question – TK-421 Feb 13 '20 at 09:49
  • Please add your framework version as well. Is your unit test run on the same framework version? – Louis Go Feb 13 '20 at 09:50
  • @LouisGo My application is .NET Framework 4.6.1 and MSTests are .NET Core 2.1 – TK-421 Feb 13 '20 at 09:50
  • And you just answered your own question. You can't expect different frameworks have the same outcome. – Louis Go Feb 13 '20 at 09:54
  • @LouisGo what does that mean? I can't use MSTests for WinForms? – TK-421 Feb 13 '20 at 09:55
  • 2
    [`ParseAddress`](https://referencesource.microsoft.com/#System/net/System/Net/mail/MailAddressParser.cs,87b2cacf203ba13e) in .NET Framework has quite different implementation than in [.NET Core](https://source.dot.net/#System.Net.Mail/Common/System/Net/Mail/MailAddressParser.cs,ec5a62b2e40e5590) – Pavel Anikhouski Feb 13 '20 at 09:56
  • @TK-421 `EmailAddressAttribute` is also looks very different, compare [.NET Framework](https://referencesource.microsoft.com/#System.ComponentModel.DataAnnotations/DataAnnotations/EmailAddressAttribute.cs,54) regex and simple check in [.NET Core](https://source.dot.net/#System.ComponentModel.Annotations/System/ComponentModel/DataAnnotations/EmailAddressAttribute.cs,c3ae85dfd8a9f58c) You may have a look at existing [thread](https://stackoverflow.com/questions/1365407/c-sharp-code-to-validate-email-address) for some examples of your own regex – Pavel Anikhouski Feb 13 '20 at 10:06
  • Yes so this is the difference in behaviour between .Net 4.6.1 and later versions (including .Net core). – Matthew Watson Feb 13 '20 at 10:19
  • Here we go: https://www.zpqrtbnk.net/posts/net-framework-4-7-2-emailaddressattribute-validation/ – Matthew Watson Feb 13 '20 at 10:22
  • @MatthewWatson so I have too old version of .net in my project (4.6.1) and that is why it returns that "test@test" is false? – TK-421 Feb 13 '20 at 12:12
  • 1
    @TK-421 It's not really that it's "too old", it's just that the behaviour has changed between that version of .Net and the version you're running the unit tests with. – Matthew Watson Feb 13 '20 at 13:05
  • @MatthewWatson Thank you very much. – TK-421 Feb 13 '20 at 14:16

0 Answers0