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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:04:56+00:00 2026-06-01T02:04:56+00:00

I have the following interfaces. I’m not sure how I can use Moq to

  • 0

I have the following interfaces. I’m not sure how I can use Moq to mock up an IRepository due to the fact that T is generic. I’m sure there’s a way, but I haven’t found anything through searching through here or google. Does anybody know how I can achieve this?

I’m fairly new to Moq, but can see the benefit of taking the time to learn it.

    /// <summary>
    /// This is a marker interface that indicates that an 
    /// Entity is an Aggregate Root.
    /// </summary>
    public interface IAggregateRoot
    {
    } 


/// <summary>
    /// Contract for Repositories. Entities that have repositories
    /// must be of type IAggregateRoot as only aggregate roots
    /// should have a repository in DDD.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IRepository<T> where T : IAggregateRoot
    {
        T FindBy(int id);
        IList<T> FindAll();
        void Add(T item);
        void Remove(T item);
        void Remove(int id);
        void Update(T item);
        void Commit();
        void RollbackAllChanges();
    }
  • 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-01T02:04:57+00:00Added an answer on June 1, 2026 at 2:04 am

    Shouldn’t be a problem at all:

    public interface IAggregateRoot { }
    
    class Test : IAggregateRoot { }
    
    public interface IRepository<T> where T : IAggregateRoot
    {
        // ...
        IList<T> FindAll();
        void Add(T item);
        // ...
     }
    
    class Program
    {
        static void Main(string[] args)
        {
            // create Mock
            var m = new Moq.Mock<IRepository<Test>>();
    
            // some examples
            m.Setup(r => r.Add(Moq.It.IsAny<Test>()));
            m.Setup(r => r.FindAll()).Returns(new List<Test>());
            m.VerifyAll();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have the following interfaces that I need to create. Can you just
I have the following interfaces that are part of an existing project. I'd like
I have the following interface that returns the generic parameter of type T using
Can anyone suggest a way to genericise the following interfaces so I can have
I have the following two interfaces, which are not part of an inheritance hierarchy.
I have the following Interfaces: public interface ITemplateItem { int Id { get; set;
I have the following two interfaces: public interface IMembershipProvider { object Login(ILoginProviderParameters loginParameters); void
In my project I have the following three interfaces, which are implemented by classes
I currently have interfaces much like the following: interface IService { void Start(); IHandler
Lets say i have the following set of interfaces.... public interface IStart { void

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.