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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:13:05+00:00 2026-06-12T06:13:05+00:00

Imagine: I want to create new functionality: Scan a file for virus. Using TDD

  • 0

Imagine: I want to create new functionality: Scan a file for virus.

Using TDD i want to make the following tests:

  • ScanningAnInfectedFileReturnsFalse

  • ScanningACleanFileReturnsTrue

In code i want to create this function:

/// <summary>
    /// 
    /// </summary>
    /// <param name="fullFileName">The File To Scan</param>
    /// <returns>True: File is clean, False: File contains virus</returns>
    public static bool ScanFile(string fullFileName)
    {
        //TODO: Scan the given FullFileName
        throw new NotImplementedException();
    }

My Question: How can i implement the two tests using the given function? I think the function should be refactored to make it testable?

  • 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-12T06:13:06+00:00Added an answer on June 12, 2026 at 6:13 am

    The problem you have is that this method ScanFile by taking a path to the file, has too many responsibilities which makes it hard to test.

    • Open the file (and deal with any errors)
    • Read the contents of the file
    • Determine if there is anthing wrong with the contents *
    • Close the file

    With the item annotated * being really all that this method should do. In this way your method would be far more testable.

    One possible way to implement this change is to have the method take an interface:

    public bool ScanFile(IScannable file)
    {
    
    }
    

    Then, say your scan method is looking for specific sequences of bytes the interface could be as simple as:

    public interface IScannable
    {
        byte[] GetContent();
    }
    

    Your testing could then simply mock this interface (See Rhino, NMock etc) for your 2 tests, one which returns a bad sequence of bytes, one which returns good. Your real implementation would be as simple as having a concrete class:

    public class ScannableFile : IScannable
    {
        private string fullFilename;
        public ScannableFile(string fullFilename)
        {
            this.fullFileName = fullFilename;
        }
    
        public byte[] GetContent()
        {
            return File.ReadAllBytes(this.fullFilename);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine that I want to create an array from another array like this: $array
I want to create a helper method that I can imagine has a signature
Imagine I want to draw a pyramid made of triangles. -Should I create a
Let's imagine I want to make a templated function that returns the first element
Imagine a doube list like the follow List<double> lstDouble=new List<double>{4,6,2,7,1,1}; So what i want
Say I have a vector with values [1,2,3,4,5,6,7,8,9,10]. I want to create a new
Let's imagine we have several type of elements, and we want to create a
Imagine I want to create an application which is very similar to MS Word
I want to create recurring events using the Ice Cube gem in Rails -
Imagine that you want to send any Serializable class to the client side of

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.