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

  • Home
  • SEARCH
  • 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 9189033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:09:15+00:00 2026-06-17T20:09:15+00:00

I have a generic method on a base class for managing list items: public

  • 0

I have a generic method on a base class for managing list items:

public abstract class BaseViewModelHandler<T> : BaseLogger, IViewModelHandler<T>
    where T : IViewModel
{
    public abstract void Handle(T viewModel);

    protected ISet<TE> ManageListItems<TE>(int[] selectedItemIds, ISet<TE> list, IService<TE> service)
        where TE : IEntity
    {
        try
        {

... param checking and code to remove unwanted items not included for brevity

            if (selectedItemIds.Any())
            {
                var itemIdsToAdd = selectedItemIds.Where(id => list.All(x => x.Id != id)).ToList();
                if (itemIdsToAdd.Any())
                {
                    foreach (var id in itemIdsToAdd)
                    {
                        var item = service.GetById(id);
                        list.Add(item);
                    }
                }
            }

            return list;
        }

...catch block removed for brevity

    }
}

I have a class that inherits this base class.

public class ConcreteViewModelHandler : BaseViewModelHandler<ConcreteViewModel>
{
    private readonly IMyService _myService;
    private readonly IMyListItemsService _myListItemsService

    public ConcreteViewModelHandler (IMyService myService, IMyListItemsService myListItemsService)
    {
        _myService = myService;
        _myListItemsService = myListItemsService
    }

    public override void Handle(ConcreteViewModel viewModel)
    {
        try
        {
            var myEntity = viewModel.Id.HasValue
                               ? _myService.GetById(viewModel.Id.Value)
                               : new MyEntity();

            myEntity.ListToManage = ManageListItems(viewModel.SelectedItemIds, myEntity.ListToManage, _myListItemsService);

...create/update removed for brevity

        }

...catch block removed for brevity

    }
}

I am trying to test the Handle method. In my test I mock the service, and I setup the ‘GetById’ method to return a new instance of my class

_myListItemsServiceMock = new Mock<IMyListItemsService>();
_myListItemsServiceMock
      .Setup(s => s.GetById(It.IsAny<int>()))
      .Returns(new MyListItem{ Id : 1});

My test does not pass, when I step through the code the call to service.GetById(id); in my base class returns null. This is the first project on which I have used Moq properly so I fear my inexperience with the framework has me missing something.

Any help would be greatly appreciated.

As requested adding the TestFixture…

[TestFixture]
public class ConcreteViewModelHandlerTestFixture
{
    private Mock<IMyService> _myServiceMock;
    private Mock<IMyListItemsService> _myListItemsServiceMock;

    #region Test Data

    private readonly ConcreteViewModel _viewModel = new ConcreteViewModel
    {
        Id = 1,
    };

    #endregion

    [SetUp]
    public void SetUp()
    {
        _myServiceMock = new Mock<IMyService>();
        _myServiceMock
            .Setup(s => s.GetById(It.IsAny<int>()))
            .Returns(new MyEntity { Id: 1});

        _myListItemsServiceMock = new Mock<IMyListItemsService>();
        _myListItemsServiceMock
            .Setup(s => s.GetById(It.IsAny<int>()))
            .Returns(new MyListItem{ Id : 1});
    }


    [Test]
    public void _the_handle_method_works()
    {
        //arrange
        var handler = new ConcreteViewModelHandler(_myServiceMock.Object, _myListItemsServiceMock.Object);

        // act
        handler.Handle(_viewModel);

        // assert
        // no assertions yet as the code falls over
    }
}

Adding more detail as requested by @Quinton Bernhardt

interface:

public interface IEntity
{
    int Id { get; }
    //Guid Guid { get; }
    int UpdateId { get; set; }
}

base class:

public abstract class BaseEntity : IEntity
{
    protected BaseEntity()
    {
    }

    protected BaseEntity(int id)
    {
        Id = id;
    }

    public virtual int Id { get; protected internal set; }
    public virtual int UpdateId { get; set; }
}

and the MyListItem class

public class MyLIstitem : BaseEntity
{
    public MyLIstitem ()
    {
    }

    public MyLIstitem (int id)
        : base(id)
    {
    }


    public virtual string Name { get; set; }
    public virtual string Value { get; set; }
}
  • 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-17T20:09:16+00:00Added an answer on June 17, 2026 at 8:09 pm

    Your problem description says:

    My test does not pass, when I step through the code the call to
    service.GetById(id); in my base class returns null.

    So it must be the call to ManageListItems<TE>() that fails on the line:

    var item = service.GetById(id);
    

    Hopefully i’m correct as it’s an assumption [using the information provided].

    Does IMyListItemService implement IService<IEntity> ? ‘cos your code implies it.

    The only weirdness i can see is that the last parameter of ManageListItems<TE>() expects a parameter of type IService<TE> where TE is IEntity which is used to make the call to service.GetById(id).

    protected ISet<TE> ManageListItems<TE>(int[] selectedItemIds, ISet<TE> list, IService<TE> service)
    

    stay with me…, it makes sense later on…

    But the object you supply to that method from your concrete class is of type IMyListItemsService as can be seen from the following in the Handle method:

    myEntity.ListToManage = ManageListItems(viewModel.SelectedItemIds, myEntity.ListToManage, _myListItemsService);
    

    what i think is happening… your setup of GetById should be on the IService instead of the IMyListItemsService

    When setting up the GetById on the on the list service, instead of this:

        _myListItemsServiceMock
            .Setup(s => s.GetById(It.IsAny<int>()))
            .Returns(new IMyListItem{ Id : 1});
    

    try

        _myListItemsServiceMock.As<IService>()
            .Setup(s => s.GetById(It.IsAny<int>()))
            .Returns(new IMyListItem{ Id : 1});
    

    or

        _myListItemsServiceMock.As<IService<IEntity>>()
            .Setup(s => s.GetById(It.IsAny<int>()))
            .Returns(new IMyListItem{ Id : 1});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have confusing situation. Base Generic Type and successor public abstract class BaseType<TEntity> :
I have a generic method: (simplified) public class DataAccess : IDataAccess { public List<T>
I have a base class that has an abstract method which returns a list
In my base class I have a generic method (ideally this would be a
I have forgotten the syntax for a generic method: public static void swap <T>
I have a generic method in a base class similar to the following: protected
If I have a base class like this that I can't change: public abstract
Assuming I have the following classes in an application: A base class: public abstract
I have a generic abstract base class with a service reference. When I try
I have a base class which contains an abstract/MustInherit method. I want this method

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.