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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:37:45+00:00 2026-05-16T15:37:45+00:00

I’ve the following simplified code which describes my problem: public interface IMyUser { int

  • 0

I’ve the following simplified code which describes my problem:

public interface IMyUser
{
    int Id { get; set; }
    string Name { get; set; }
}

Which is used in the dataccess layer like this:

public interface IData
{
    T GetUserById<T>(int id) where T : IMyUser, new();
}

The userlogic class is defined as follows:

public class UserLogic
{
    private IData da;

    public UserLogic(IData da)
    {
        this.da = da;
    }

    public IMyUser GetMyUserById(int id)
    {
        return da.GetUserById<MyUser>(id);
    }
}

The userlogic uses a MyUSer class which is only visible internally.

I want to use Moq to mock the call to the dataaccess layer. But becuase I cannot access the MyUser class from my unit test code (which is as designed) , I don’t know how to setup moq?

The Moq code should be something like:

var data = new Mock<IData>();
data.Setup(d => d.GetUserById<MyUser ???>(1)).Returns(???);

var logic = new UserLogic(data.Object);
var result = logic.GetMyUserById(1);

How to solve this?

  • 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-16T15:37:45+00:00Added an answer on May 16, 2026 at 3:37 pm

    Let me just expand on Sjoerd’s answer. The problem you are facing is due to not being able to access MyUser type from the test assembly. That problem is easily fixed with InternalsVisibleTo assembly attribute.

    I would however recommend to rethink your design and get rid of IMyUser interface and instead just use MyUser class (which should be public). Normally you put services behind interfaces, not entities. Are there any good reasons for providing multiple implementations of IMyUser?

    Have a look at how much cleaner this implementation is:

    public interface IData
    {
        MyUser GetUserById(int id);
    }
    
    public class UserLogic
    {
        private IData da;
    
        public UserLogic(IData da)
        {
            this.da = da;
        }
    
        public MyUser GetMyUserById(int id)
        {
            return da.GetUserById(id);
        }
    }
    
    internal class MyUser {
        int Id { get; set; }
        string Name { get; set; }
    }
    

    There is another solution, if you insist on having IMyUser interface and its internal implementation. Your existing solution, if I infer the contents of IData.GetUserById<T> correctly, goes something like this:

    public class UserData : IData {
        T GetUserById<T>(int id) where T : IMyUser, new(){
           T returned = new T();
           //fill in properties
           returned.Name = "test";
           return returned;
        }
    }
    

    The above code is a slight violation of SRP(warning, PDF) and mixes two responsibilities – retrieving an entity from persistent storage and creating an instance of the entity. Not only that, it also puts the creation responsibility on the interface, which is even worse.

    Decoupling those responsibilities using Abstract Factory and Dependency Injection(PDF) patterns will lead to much cleaner design that does not suffer from the same problem as before.

    public interface IMyUserFactory {
        IMyUser Create();
    }
    
    public interface IData
    {
        IMyUser GetUserById(int id);
    }
    
    internal MyUserFactory : IMyUserFactory {
       public IMyUser Create() {return new MyUser();}
    }
    
    internal class UserData : IData {
    
        IMyUserFactory m_factory;
        public UserData(IMyUserFactory factory) {
           m_factory = factory;
        }
    
        public IMyUser GetUserById(int id) {
           IMyUser returned = m_factory.Create();
           //fill in properties
           returned.Name = "test";
           return returned;
        }
    }
    
    //and finally UserLogic class
    public class UserLogic
    {
        private IData da;
    
        public UserLogic(IData da)
        {
            this.da = da;
        }
    
        public IMyUser GetMyUserById(int id)
        {
            return da.GetUserById(id);
        }
    }
    
    //The test then becomes trivial
    [TestMethod]
    public void Test() {
      var data = new Mock<IData>();
      data.Setup(d => d.GetUserById(1)).Returns(new Mock<IMyUser>().Object);
    
      var logic = new UserLogic(data.Object);
      var result = logic.GetMyUserById(1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace

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.