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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:39:31+00:00 2026-06-18T04:39:31+00:00

I’ve recently started writing unit tests for my codes using NUnit framework. I am

  • 0

I’ve recently started writing unit tests for my codes using NUnit framework.
I am familiar with basic concepts of NUnit and writing simple tests.
Actually I don’t know how to test codes that work with files :
for example I want to write test for below class :

  public class ShapeLoader
    {
        private static void StreamLoading(object sender, StreamLoadingEventArgs e)
        {
            try
            {
                string fileName = Path.GetFileName(e.AlternateStreamName);
                string directory = Path.GetDirectoryName(e.AlternateStreamName);

                e.AlternateStream = File.Exists(directory + @"\\" + fileName) ? new FileStream(directory + @"\\" + fileName, e.FileMode, e.FileAccess) : null;
            }
            catch
            { }
        }

        public static ShapeFileFeatureLayer Load(string filePath, ShapeFileReadWriteMode shapeFileReadWriteMode, bool buildIndex = true)
        {
            if (!File.Exists(filePath)) { throw new FileNotFoundException(); }
            try
            {
                switch (shapeFileReadWriteMode)
                {
                    case ShapeFileReadWriteMode.ReadOnly:
                        //  if (buildIndex && !HasIdColumn(filePath)) BuildRecordIdColumn(filePath, BuildRecordIdMode.Rebuild);

                        ShapeFileFeatureLayer.BuildIndexFile(filePath, BuildIndexMode.DoNotRebuild);

                        var shapeFileLayer = new ShapeFileFeatureLayer(filePath, shapeFileReadWriteMode) { RequireIndex = true };
                        ((ShapeFileFeatureSource)shapeFileLayer.FeatureSource).StreamLoading += StreamLoading;
                        return shapeFileLayer;

                    case ShapeFileReadWriteMode.ReadWrite:
                        return new ShapeFileFeatureLayer(filePath, shapeFileReadWriteMode);

                    default:
                        return null;
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Could not find file")) throw new FileNotFoundException();
                throw;
            }
        }
    }

This code needs to physical file to check if it works fine or not , but Is this right that unit test has dependency with physical files ?

How do I write unit tests for codes like this ?

  • 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-18T04:39:32+00:00Added an answer on June 18, 2026 at 4:39 am

    Unit tests should not have any dependencies to external resources such as file system or database, etc.
    in these case you must use mocking frameworks such as Moq or Rhino Mock.
    If you want to test your code and it’s external dependencies you should write Integration Test.

    So In your case, if you don’t want to use any mocking framework, you can create your own fake classes for dependencies and pass them by Dependency Injection Pattern

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

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am using JSon response to parse title,date content and thumbnail images and place
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.