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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:52:28+00:00 2026-05-28T16:52:28+00:00

Lets say I have the below method called DoSomething. When writing unit tests how

  • 0

Lets say I have the below method called DoSomething.

When writing unit tests how do I know whether to use a Fake, Mock or Override approach and why is one better than the other?

public List<MyClass> DoSomething()
{
  List<MyClass> data = GetData();

  if (data.Count == 0)
    return new List<MyClass>();

  data = GetFormattedData(data);

  if (data.Count == 0)
    return new List<MyClass>();

  return data;
}

[Test]
public void DoSomething_NoData_ReturnsEmptyList()
{
  //Change method parameters to pass in IDataProvider that exposes GetData method
  //Create FakeProvider class implementing IDataProvider
  //Ensure FakeProvider.GetData returns no data

  //Create FakeClass that inherits class from DoSomething class
  //Make FakeClass.GetData return no data
  //When DoSomething is called in the test it will call the parent class

  //Create Mock of class that DoSomething/GetData/GetFormattedData is in
  //Tell mock to make sure GetData returns empty list
  //Call DoSomething in test
}

[Test]
public void DoSomething_NoFormattedData_ReturnsEmptyList()
{
   //Same possibilities exist as above
}
  • 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-28T16:52:29+00:00Added an answer on May 28, 2026 at 4:52 pm

    In this case you could supply your function with the data so that the function signature would be public List<MyClass> DoSomething(List<MyClass data) Then your function would only have a single responsibility and that would be to format the data.

    If you still want to do the data fetch withing your function and you will be accessing a database or external service I would use dependency injection and mocking to test the function.

    Generally it is good to avoid dependencies if possible. It all depends of course, if you don’t do any other data fetching in your class then it’s just a matter of how far down you will send your dependency.

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

Sidebar

Related Questions

Lets say we have a method below. How would you implement auhorization that would
Let's say I have a utility class DateUtil (see below). To use this method
Lets say I have model inheritance set up in the way defined below. class
Lets say I have a table in a sql server 2000 database called TransactionType:
Lets say you have an action method to display products in a shopping cart
In general , let's say you have a method like the below. def intersect_two_lists(self,
Lets say I have the below: <Style TargetType={x:Type TextBox}> <Setter Property=BorderThickness Value=1 /> <Setter
Lets say I have this program below. Now I want to pass both the
Lets say I have an interface passed to my method: public void AlphaToChar(iList _blah)
I have a basic PHP question, take the code below for example, let's say

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.