Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7831243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:38:06+00:00 2026-06-02T11:38:06+00:00

I have a simple method for sending emails: public void notifyEmail(string messageSubject, string messageBody)

  • 0

I have a simple method for sending emails:

public void notifyEmail(string messageSubject, string messageBody)
{
   MailMessage message = new MailMessage(from, to);

   message.Subject = messageSubject;
   message.Body = messageBody;

   SmtpClient client = new SmtpClient(smtp_client);
   client.Send(message);

   message.Dispose();//release everything related
}

And a unit test (I’m learning):

[TestMethod()]
    public void notifyEmailTest()
    {
        eMail target = new eMail("TEST Subject","TEST Body"); // TODO: Initialize to an appropriate value

        bool testSent = true;
        try
        {
            target.notifyEmail();
        }
        catch (Exception)
        {
            testSent = false;
        }

        Assert.IsTrue(testSent);            
    }

I deliberately set the smtp_client variable value to something invalid.

Running the code in my project results in an error.

Running the test method results in a Pass. Should my test or method be structured differently so that errors will fail the test?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:38:12+00:00Added an answer on June 2, 2026 at 11:38 am

    I always do everything I can to avoid putting try-catch clauses on my unit tests. Instead try using the ExpectedException attribute (the attribute is the same for NUnit and MSTest) and set the type to the exception you are expecting i.e.

    [TestMethod]
    [ExpectedException(typeof(NetworkException))]
    public void ShouldThrowNetworkExceptionIfSmtpServerIsInvalid)
    {
         //... test code here.
    }
    

    Another approach that I have used is to create a static class with an AssertExpectedException method since sometimes a method can throw the same type of exception for different reasons and the only way to know for sure if the accurate message is being returned is with custom code since the attribute does not assert the message the thrown exception is returning.

    Hope this helps.

    Regards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have this simple method: public IEnumerable<uint> GetNumbers() { uint n = 0;
I have this code: public void AddNode(string Node) { try { treeView.Nodes.Add(Node); treeView.Refresh(); }
Hi I have this simple method which gets triggered when i click inside a
I am simply learing Ajax with jQuery and have a simple page method that
I was wondering if anyone knew why jQuery doesn't have a simple $().id() method.
I have only simple data types in method signature of service (such as int,
I have a simple mapview that has the following viewdidload method and didupdate to
Say I have a simple client/server scenario with one method: // client code $client
I have a simple jigsaw puzzle i'm making. I have a method that is
Let's say I have a simple ASP MVC3 list controller, with an add method,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.