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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:36:14+00:00 2026-06-01T17:36:14+00:00

I am using Moq framework for Mocking. I have a SocialWorker which derives from

  • 0

I am using Moq framework for Mocking.

I have a SocialWorker which derives from an abstract DataWorker class.

The SocialWorker has reference to couple of Repositories as a virtual property

I am trying to run the following test:

private Place _place;
private Mock<IRepository<Resources.Data.Place>> _placeRepositoryMock;
private Mock<SocialWorker> _socialWorkerMock;

[SetUp]
public void SetUp()
{
    _place = new Place {Name = "A"};

    _socialWorkerMock = new Mock<SocialWorker> {DefaultValue = DefaultValue.Mock};

    IRepository<Resources.Data.Place> placeRepository = _socialWorkerMock.Object.PlaceRepository;

    _placeRepositoryMock = Mock.Get(placeRepository);

    _placeRepositoryMock.Setup(
        repository =>
        repository.Find(It.IsAny<Expression<Func<Resources.Data.Place, bool>>>(), null, null))
        .Returns(new[] {new Resources.Data.Place()});
}

[Test]
public void AddShouldAddANewPlace()
{
    var placeManager = new PlaceManager(_socialWorkerMock.Object);

    object placeEntity = placeManager.Add(_place);

    placeEntity.GetType().Should().Equal(typeof (Resources.Data.Place));

    _socialWorkerMock.Verify(
        socialWorker => socialWorker.PlaceRepository.Add(It.IsAny<Resources.Data.Place>()), Times.Once());

    _placeRepositoryMock.Verify(
        placeRepository =>
        placeRepository.Find(p => p.Name.Equals(_place.Name), null, null).First(),
        Times.Once());
}

The last verification of this test fails with this error:

System.NotSupportedException : Invalid verify on a non-virtual (overridable in VB) member: placeRepository => placeRepository.Find(p => p.Name.Equals(._place.Name), null, null).First<Place>()

I am fairly new to Moq and unit testing in general.

Here is the relevant code for your reference:

IUnitOfWork

public interface IUnitOfWork : IDisposable
{
    void CommitChanges();
}

IRepository

public interface IRepository<T>
{
    void Add(T entity);

    void Delete(T entity);

    IEnumerable<T> Find(Expression<Func<T, bool>> filter = null,
                        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
                        IList<string> includedProperties = null);

    T FindById(object id);

    void Update(T entity);
}

DataWorker

public abstract class DataWorker : IUnitOfWork
{
    protected ObjectContext ObjectContext;
    private bool _disposed;

    protected DataWorker()
    {
        ObjectContext = new ObjectContext(ConfigurationManager.ConnectionStrings["DataEntities"].ConnectionString);
    }

    ~DataWorker()
    {
        . . .
    }

    protected virtual void Dispose(bool disposing)
    {
        . . .
    }

    public virtual void CommitChanges()
    {
        ObjectContext.SaveChanges();
    }

    public void Dispose()
    {
        . . .
    }
}

DataRepository

public class DataRepository<T> : IRepository<T> where T : EntityObject
{
    private readonly ObjectSet<T> _objectSet;

    public DataRepository(ObjectContext objectContext)
    {
            _objectSet = objectContext.CreateObjectSet<T>();
    }

    public void Add(T entity)
    {
        . . .
    }

    public void Delete(T entity)
    {
        . . .
    }

    public virtual IEnumerable<T> Find(Expression<Func<T, bool>> filter = null,
                               Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
                               IList<string> includedProperties = null)
    {
        . . .
    }

    public T FindById(object id)
    {
        . . .
    }


    public void Update(T entity)
    {
        . . .
    }
}

SocialWorker

public class SocialWorker : DataWorker
{
    private IRepository<ContactRequest> _contactRequestRepository;
    private IRepository<Place> _placeRepository;
    private IRepository<User> _userRepository;

    public virtual IRepository<ContactRequest> ContactRequestRepository
    {
        get
        {
            return _contactRequestRepository ??
                   (_contactRequestRepository = new DataRepository<ContactRequest>(ObjectContext));
        }
    }

    public virtual IRepository<Place> PlaceRepository
    {
        get { return _placeRepository ?? (_placeRepository = new DataRepository<Place>(ObjectContext)); }
    }

    public virtual IRepository<User> UserRepository
    {
        get { return _userRepository ?? (_userRepository = new DataRepository<User>(ObjectContext)); }
    }
}
  • 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-01T17:36:15+00:00Added an answer on June 1, 2026 at 5:36 pm

    I’m confused about what your code is trying to do, the design seems a bit odd.

    Firstly, the test you have a problem with is referencing a class PlaceManager, which you haven’t specified and as that’s part of the test, it’s difficult to see quite where it’s going wrong.

    Furthermore, I’m not sure what PlaceManager is for. Your design has SocialWorker with a property providing the PlaceRepository, so what is the PlaceManager for, and why is it being passed a SocialWorker? Doesn’t SocialWorker manage its own PlaceRepository?

    Why aren’t you calling _socialWorker.PlaceRepository.Add(place), or rather more neatly _socialWorker.Add(place)?

    I’m only guessing that PlaceManger accesses the SocialWorker.PlaceRepository , but I don’t see why as the SocialWorker.PlaceRepository is a public property, there’s nothing to stop anyone using directly and ignoring the PlaceManager. That’s if the PlaceManager is also some sort of Repository for Place.

    Also, your IRepository code has Add() as a void, yet the PlaceManager returns an object. Why is this not strongly-typed to Place? It seems that is what you’re expecting… And your PlaceManager.Add() method returns a Place, though the Repository Add() method is void. (is it the same place after it has been persisted to the context?) Is your PlaceManager doing something else with the Place?

    Perhaps I have the wrong end of the stick? Could you clarify more what the intent of your code is, over what it does? Sorry for so many questions, but I’d like to know what your design is supposed to be doing before figuring out why you’re trying to verify against a non-virtual…

    I think the cause of the specific problem, is that in your verify you’re expecting a verify against the Find, however, that includes a call to First(), which needs to be mocked and can’t be, as it’s a static extension method, not virtual or abstract.

    I’m not sure why you’re calling against First(), anyway. The method you’re verifying is just:

    _placeRepositoryMock.Verify(placeRepository =>
       placeRepository.Find(p => p.Name.Equals(_place.Name), null, null),
       Times.Once());
    

    That’s what will actually be called, as that’s the signature of the method in the repository. You can only check that the interface method is called.

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

Sidebar

Related Questions

I am using Moq as my mocking framework. As per the code below, I
We are using Moq as our mocking framework, the problem is that type that
I have a class which has a internal method and i want to mock
I've been using Moq framework in c# for mocking in unit tests however there
I am using Moq as my mocking framework and I need to test a
I have a controller in C# using the ASP.Net MVC framework public class HomeController:Controller{
I have been using Moq for my mocking needs the last years, but after
I have some code in a test using Moq: public class Invoice { ...
I have an object I'm trying to mock using moq. The object's constructor has
I am using the Moq framework for unit testing and would like to be

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.