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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:37:27+00:00 2026-05-19T01:37:27+00:00

In answer to this question here Jon Skeet answered :>> You can test via

  • 0

In answer to this question here Jon Skeet answered :>>

You can test via mocking really easily (without having to mock classes, which gets ugly)

I would like a better understanding through a standalone example elaborating this aspect …both the before (ugly) and the after (interface-based) scenarios in C#.

And also, in addition, if there is an example within the .NET Framework BCL itself that would be great

  • 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-05-19T01:37:27+00:00Added an answer on May 19, 2026 at 1:37 am

    Say you have this method to fetch all names of people from a file:

    string[] GetNamesFrom(string path) { }
    

    To test this method you would have to supply a path name of an existing file, which requires some setup.

    Compare that to this method:

    string[] GetNamesFrom(IFile file)
    

    If IFile contains a GetContents() method, then your “real” implementation of this interface could access the file system, and your mock class could simply return your test input data.

    Using a mock library like moq (http://code.google.com/p/moq/) this becomes really simple:

    var fileMock = new Mock<IFile>();
    fileMock.Setup(f => f.GetContents()).Returns(testFileContents));
    Assert.Equals(expectedNameArray, GetNamesFrom(fileMock.Object));
    

    Writing a file to the file system prior to testing might not sound like alot of setup, but if you’re running alot of tests, it becomes a mess. By using interfaces and mocking, all setup happens within your test method.

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

Sidebar

Related Questions

This may be a simple question but I can;t find the answer anywhere. Here
I know I can answer this question easily for myself by generatin the code
I am having a problem very similar to this question here: Can I use
Answer Summary: Solved this problem using Jon Skeet's answer below. Here is the finished
I read this interesting line here , in an answer by Jon Skeet. The
I'm new to rails and I guess you can answer this question easily. What
I've spent hours searching the web for an answer to this question... Here's what
This question has developed off an answer here . My question therefore is what
This question already has an answer here: view sql that linq-to-sql produces 3 Answers
This question already has an answer here: What are Extension Methods? 5 answers Usage

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.