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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:32:33+00:00 2026-05-28T05:32:33+00:00

[using Moq] I am trying to mock a concrete class and mock a virtual

  • 0

[using Moq]

I am trying to mock a concrete class and mock a virtual method “Get()” of that class. When testing a method “GetItemsNotNull()” I always get returned null, instead of the return of the mocked function.

Here is the code


//SomeClasses.cs
namespace MoQExamples
{
    public abstract class Entity
    {

    }

    public class Abc : Entity
    {

    }

    public interface IRepository<T> where T : Entity
    {
        IQueryable<T> Get();
    }

    public class Repository<T> : IRepository<T> where T : Entity
    {
        private readonly ISession _session;

        public Repository()
        {
            _session = null;
        }

        public Repository(ISession session)
        {
            _session = session;
        }

        protected ISession CurrentSession
        {
            get { return _session; }
        }

        public virtual IQueryable<T> Get()
        {
            return CurrentSession.Query<T>();
        }

    }

    public interface IAbcRepository
    {
        Abc GetItemsNotNull();
    }

    public class AbcRepository : Repository<Abc>, IAbcRepository
    {
        public Abc GetItemsNotNull()
        {
            return Get().FirstOrDefault(abc => abc !=null);
        }
    }
}

and here are the test class

namespace MoQExamples
{
    [TestFixture]
    public class SomeClassesTest
    {
        private readonly Mock<AbcRepository> _abcRepositoryMock = new Mock<AbcRepository>(MockBehavior.Strict) { CallBase = true };

        [SetUp]
        public void SetupTest()
        {
            _abcRepositoryMock.Setup(x => x.Get()).Returns(Get);
        }

        public IQueryable<Abc> Get()
        {
            return (new List<Abc>() { new Abc() }) as IQueryable<Abc>;
        }

        [Test]
        public void TestGetItemsNotNull()
        {
            Assert.IsNotNull(_abcRepositoryMock.Object.GetItemsNotNull());
        }

    }
}

the assert alays fails..instead of returning the SomeClassesTest.Get()

thanks for advance guys!

  • 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-28T05:32:33+00:00Added an answer on May 28, 2026 at 5:32 am

    I suspect this is the problem:

    return (new List<Abc>() { new Abc() }) as IQueryable<Abc>;
    

    List<T> doesn’t implement IQueryable<T>, so this will always return null. Call AsQueryable to convert it instead:

    return new List<Abc>().AsQueryable();
    

    As an aside, this is a reason to prefer casts over as in most situations: if you’d just cast to IQueryable<Abc>, you’d have received an exception at the line which was really causing the problem. You should only use as when it’s not a bug for the conversion to “fail”. An as operator should almost always be followed by a nullity test.

    (Note that this behaviour in itself has nothing to do with mocking or Moq. It’s just the behaviour of the as operator…)

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

Sidebar

Related Questions

I am using trying to get my head round unit testing with DI/Mocks (Ninject/Moq)
I'm using MOQ to mock a method call with an expected return list. My
I am using Moq for unit testing, and I am trying to write my
I'm trying to work out how this Mock (using the Moq framework) stuff all
I am trying to learn TDD/BDD using NUnit and Moq. The design that I
I have an object I'm trying to mock using moq. The object's constructor has
I am using Moq to mock my Repository layer so I can unit test.
Trying to write Unit test for Silverlight 4.0 using Moq 4.0.10531.7 public delegate void
I´m encountering this problem trying to mock some objects that receive complex lambda expressions
I'm trying to test/spec the following action method public virtual ActionResult ChangePassword(ChangePasswordModel model) {

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.