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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:40:39+00:00 2026-05-13T12:40:39+00:00

Consider this programs public class ItemManager { private ItemFetcher itemFetcher; public ItemManager(ItemFetcher _itemFetcher) {

  • 0

Consider this programs

public class ItemManager
{
    private ItemFetcher itemFetcher;
    public ItemManager(ItemFetcher _itemFetcher)
    {
        itemFetcher = _itemFetcher;
    }
    public List<Item> GetItemsFomTable()
    {
        List<Item> itemsList = new List<Item>();
        Item item;

        DataTable resultDataTable = itemFetcher.GetItemsFromDB();

        foreach (DataRow row in resultDataTable.Rows)
        {
            item = new Item();
            // set item's name property
            // set item's price property
            itemsList.Add(item);
        }
        return itemsList; 
    }
}
public class Item
{
    string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    double price;

    public double Price
    {
        get { return price; }
        set { price = value; }
    }
}
public class ItemFetcher
{
    public virtual DataTable GetItemsFromDB()
    {
        // DoSomething and fetch records in DataTable and return
        return new DataTable();
    }
}

I want to test itemFetcher.GetItemsFromDB method is called once in the method “GetItemsFomTable()” of the class ItemManager. Here is the Test

[TestFixture]
    public class ItemManagerTester
    {        
        [SetUp]
        public void Init()
        {

        }
        [Test]
        public void TestForGetItemsFomTable()
        {
            var mockItemFetcher = new Mock<ItemFetcher>();
            var itemManager = new ItemManager(mockItemFetcher.Object);

            mockItemFetcher.Setup(x => x.GetItemsFromDB());
            itemManager.GetItemsFomTable();

            mockItemFetcher.VerifyAll();
        }
    }

As you may see there is List initialised inside the method under test

List<Item> itemsList = new List<Item>();

I get this Exception raised:

TestCase 'MockingSample.ItemManagerTester.TestForGetItemsFomTable'
failed: System.NullReferenceException : Object reference not set to an instance of an object.
    ItemManager.cs(26,0): at MockingSample.ItemManager.GetItemsFomTable()
    ItemManager.cs(77,0): at MockingSample.ItemManagerTester.TestForGetItemsFomTable()

What should I do with the List ? How and where can I mock this if needed ?

  • 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-13T12:40:39+00:00Added an answer on May 13, 2026 at 12:40 pm

    Since you haven’t specified a return value, the mock item fetcher returns null, so the attempt to access resultDataTable.Rows throws.

    To fix the error, tell Moq what you want it to return when you configure the expectation:

    mockItemFetcher.Setup(x => x.GetItemsFromDB()).Returns(new DataTable());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this class: public class Foo { // Fields private string _bar; // Properties
Consider this code sample: public class Human { public string Value { get; set;}
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
Consider this code: class First { public: int y; First() { y = 90;
Consider this Method in a DatabaseHelper class: public Cursor getRules() { SQLiteDatabase db=this.getReadableDatabase(); Cursor
Consider the following program: class Base { private: int m_nID; public: Base() { m_nID
Please consider a pure virtual class like this: class Foo { public: virtual int
Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>
Consider the following code parts: this is Timing.h: class Timing { public: //creates a
Consider this example code: public class A<T> { public static T TheT { get;

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.