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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:02:16+00:00 2026-05-15T09:02:16+00:00

It has been decided to write some unit tests using moq etc..It’s lots of

  • 0

It has been decided to write some unit tests using moq etc..It’s lots of legacy code c#

(this is beyond my control so cannot answer the whys of this)

Now how do you cope with a scenario when you dont want to hit the database but you indirectly still hit the database?

This is something I put together it’s not the real code but gives you an idea.

How would you deal with this sort of scenario?

Basically calling a method on a mocked interface still makes a dal call as inside that method there are other methods not part of that interface?Hope it’s clear

         [TestFixture]
            public class Can_Test_this_legacy_code
            {
                [Test]
                public void Should_be_able_to_mock_login()
                {
                    var mock = new Mock<ILoginDal>();
                    User user;
                    var userName = "Jo";
                    var password = "password";
                    mock.Setup(x => x.login(It.IsAny<string>(), It.IsAny<string>(),out user));

                    var bizLogin = new BizLogin(mock.Object);
                    bizLogin.Login(userName, password, out user);
                }
            }

            public class BizLogin
            {
                private readonly ILoginDal _login;

                public BizLogin(ILoginDal login)
                {
                    _login = login;
                }

                public void Login(string userName, string password, out User user)
                {
                    //Even if I dont want to this will call the DAL!!!!!
                    var bizPermission = new BizPermission();
                    var permissionList = bizPermission.GetPermissions(userName);

                    //Method I am actually testing
                    _login.login(userName,password,out user);
                }
            }
            public class BizPermission
            {
                public List<Permission>GetPermissions(string userName)
                {
                    var dal=new PermissionDal();
                    var permissionlist= dal.GetPermissions(userName);
                    return permissionlist;
                }
            }

            public class PermissionDal
            {
                public List<Permission> GetPermissions(string userName)
                {
                    //I SHOULD NOT BE GETTING HERE!!!!!!
                    return new List<Permission>();
                }
            }

            public interface ILoginDal
            {
                void login(string userName, string password,out User user);
            }

            public interface IOtherStuffDal
            {
                List<Permission> GetPermissions();
            }

            public class Permission
            {
                public int Id { get; set; }
                public string Name { get; set; }
            }

Any suggestions?
Am I missing the obvious?
Is this Untestable code?

Very very grateful for any suggestions.

  • 1 1 Answer
  • 1 View
  • 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-15T09:02:17+00:00Added an answer on May 15, 2026 at 9:02 am

    As it is now, BizLogin is not testable, as it directly instantiates BizPermission, which in turn instantiates PermissionDal, which then hits the DB.

    The best solution would be to refactor BizLogin to replace the direct instantiation of BizPermission with a call to a factory (method), or Dependency Injection. It is not clear to me from your post whether you can refactor the code – if so, this is the preferred solution.

    However, if refactoring is not an option, you could still try a nasty trick. This is possible in Java, I don’t know C# that well, but since the two languages are fairly similar, I guess it can be possible in C# too (although I can not fill in the exact technical details).

    You could replace the compiled class files of BizPermission with different mock implementations for your unit tests. This is of course risky, as you must make sure that the alternative implementations don’t get mixed into your production assemblies. Also it requires a bit of messing with classpaths and stuff. So only try this if refactoring is really, absolutely out of question.

    How to replace class files with test implementations

    (using Java terminology – I hope it is clear enough for C# too…) The basic idea is that the runtime is looking for classes on the classpath, and it loads the first suitable class definition it finds on the classpath. So you can create a mock implementation of BizPermission in your unit test source folder, in the exact same package and with the same interface as the original. Then have it compiled into e.g. a test-classes folder (whereas your production code is compiled into e.g. classes). Now if you set up your test classpath so that test-classes is before classes, the runtime will load the fake BizPermission class when running your tests, instead of the original, when BizLogin attempts to instantiate this class.

    Example of refactoring to use a factory method

    public class BizLogin
    {
        private readonly ILoginDal _login;
    
        public BizLogin(ILoginDal login)
        {
            _login = login;
        }
    
        protected BizPermission getBizPermission()
        {
            return new BizPermission();
        }
    
        public void Login(string userName, string password, out User user)
        {
            var bizPermission = getBizPermission();
            var permissionList = bizPermission.GetPermissions(userName);
    
            //Method I am actually testing
            _login.login(userName,password,out user);
        }
    }
    

    In test code:

    public class FakeBizPermission implements BizPermission
    {
        public List<Permission>GetPermissions(string userName)
        {
            // produce and return fake permission list
        }
    }
    
    public class BizLoginForTest
    {
        public BizLoginForTest(ILoginDal login)
        {
            super(login);
        }
    
        protected BizPermission getBizPermission()
        {
            return new FakeBizPermission();
        }
    }
    

    This way you can test the critical functionality via BizLoginForTest, with minimal changes to the original BizLogin class.

    An alternative would be to inject a full factory object, as mentioned in Jeff’s comment. This would require a bit more code changes (possibly including in the clients of BizLogin), so it’s a bit more intrusive.

    Note that the prime goal of such refactorings is always to allow unit testing, not to win a prize for the beauty of your new design 🙂 So it is best to start with the least changes to existing code which allow you to cover the functionality with tests. Once you have the tests in place, you can refactor with more confidence towards a cleaner, better design.

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

Sidebar

Related Questions

We've all been there. You have written some code and unit tests, the tests
I am implementing some networking stuff in our project. It has been decided that
I have some code I wrote a few years ago. It has been working
This has been perplexing me for a couple of days, so I decided to
this has been an ongoing problem with me, ive been trying to make a
This has been a massive headache. We use Ning as a our platform for
This has been frustrating me for a while now. I started developing a site
This has been a rather problematic issue on numerous occasions. We have alot of
This has been driving me crazy for the past few minutes I have a
This has been one of the biggest obstacles in teaching new people ColdFusion. When

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.