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 8299933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:21:13+00:00 2026-06-08T16:21:13+00:00

I have the following method to test and i have written two tests, testing

  • 0

I have the following method to test and i have written two tests, testing the scenario that an exception is thrown, and im wondering which is correct.

namespace JimBob.CsvImporter.Entity
{

    public interface IIOManager
    {
        Stream OpenFile(string path);

        TextReader ReturnReader(string path);
    }


    public class IOManager : IIOManager
    {
        public Stream OpenFile(string path)
        {
            return File.Open(path, FileMode.Open);
        }

        public TextReader ReturnReader(string filePath)
        {
            return new StreamReader(filePath);
        }
    }


public class EntityVerification
{

    private IIOManager _iomgr;

    public EntityVerification(IIOManager ioManager)
    {
        this._iomgr = ioManager;
    }

    ...

    /// <summary>
    /// Ensures user can open file.
    /// </summary>
    /// <param name="errorMessageList">A running list of all errors encountered.</param>
    public void ValidateAccessToFile(string filePath, List<string> errorMessageList)
    {
        try
        {
            using (FileStream fs = (FileStream)_iomgr.OpenFile(filePath))
            {
                if (fs.CanRead && fs.CanWrite) { }
                else
                {
                    errorMessageList.Add("Can not read/write to the specified file.");
                }
            }
        }
        catch (Exception e)
        {
            errorMessageList.Add(e.Message);
        }
    }

Tests:

    [Test]
    public void ValidateAccessToFile_CanReadWriteToFile_ThrowException()
    {
        List<String> errorMessageList = new List<string>();
        StubService stub = new StubService();
        EntityVerification testObject = new EntityVerification(stub);
        testObject.ValidateAccessToFile("ergesrg", errorMessageList);
        Assert.AreEqual(errorMessageList.Count, 0);
    }

    [Test]
    public void ValidateAccessToFile_CanReadWriteToFile_ThrowsException()
    {
        Mock<IIOManager> mock = new Mock<IIOManager>();
        mock.Setup(x => x.ReturnReader(It.IsAny<string>())).Throws(new InvalidOperation("throw baby."));
        EntityVerification testObject = new EntityVerification(mock.Object);
        List<String> errorMessageList = new List<string>();
        testObject.ValidateAccessToFile("blabla.txt", errorMessageList);
        Assert.AreEqual(errorMessageList.Count, 0);
    }



    public class StubService : IIOManager
    {
        public Exception ex;
        public Stream OpenFile(String path)
        {
            throw ex;
        }
    }

Both tests just check that a local variable, in this case errorMessageList, to the test contains something so im not sure which i should be using.

Any comments would be appreciated.

Thanks

  • 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-08T16:21:15+00:00Added an answer on June 8, 2026 at 4:21 pm

    First, shouldn’t you be checking that you do add an error message to the list?

    Assert.AreEqual(errorMessageList.Count, 1);
    

    Second, although the second one is a little less verbose, and more readable (as you don’t need to implement another class), it doesn’t matter – the two tests are both valid ways of achieving the same target. Just choose one and move on to your next feature…

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

Sidebar

Related Questions

I have the following method where I want to test the event.status property only
I have the following C# method: private string[] test() { } I would like
I have following spec to test controller method: context #create do it should redirect
I have following method which I am using to load ActiveX control dynamically, Dim
I have the following method, which takes in the name of a file as
I have the following method that returns void and I need to use it
I have the following method for which I am trying to write a unit
I have written a simple WPF application in which I wanted to test the
I have written a test ViewEngine which responds to requests with a view name
A unit test I've written is failing with the following error message. This 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.