none
How to test method which connect to database RRS feed

  • Question

  • Dear all,

    I have implemented a Test unit for my project and I need o test a method which collect user data from a database.
    The way to retrieve data is through Sql store procedure.

    The database acces is through WCF web service and in addition to retrive database data I need also to validate custom user data against data retrived.

    A sample unit test is as follow :

     [TestMethod]
            public void TestGetUsers()
            {
                UnityContainer _container = new UnityContainer();
                List<UserInfo> _infos = new List<UserInfo>();
    
                _container
                    .RegisterType<IUserRepository, UserRepository>()
                    .RegisterType<IUserData, UserData>();
                
                IUserRepository _userRepo = _container.Resolve<UserRepository>();
                _infos=_userRepo.GetUsers();
                Assert.AreEqual(7, _infos.Count);
    
    
            }

    I have read in different place that Unit Testing should not go directly to hit the database but have seen also some special case where it does.

    What is the rule in this because it seems that anyone have its own interpretation of it

    If I have a test database to validate my method why should I not hit the database ?

    If not how can I replace _userRepo.Getusers() with a Mockup then which is injected in place of the IUserRepository ?

    Thanks for clarification

    regards

    serge

    Monday, October 22, 2018 9:06 AM

Answers

  • Thanks for your friendly response, it seems that you use another account to reply this case:) am I right?

    About whether we should use the same DB during developing and testing, it is not really strict. Maybe different companies or teams have their own behaviors or requirements.

    >>But then as a developper if I have a visual stuio project with UnitTest, should I have an other Unit Test project which gets data from my test db too ?

    So for this issue, it is also opening, but we could have the measurement criteria and make sure that whether it is convenient or impact other test projects or whether we really have to use the DB or others.

    Sincerely,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by wakefun Wednesday, October 31, 2018 10:51 AM
    Friday, October 26, 2018 2:42 AM
    Moderator
  • Hi serge,

    Do you get helpful suggestions for this issue? Any latest information, feel free to share it here.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by wakefun Wednesday, October 31, 2018 10:51 AM
    Tuesday, October 30, 2018 2:00 AM
    Moderator

All replies

  • Hi Serge,

    Just provide my understanding, about how to test method which connect to the database, the real issue is that what you want to really test.

    https://stackoverflow.com/questions/1217736/how-to-write-unit-tests-for-database-calls

    I think you could also get some information from above case.

    In addition, as the test team members, we often use our own database, because if you use the same database as the developer team, it will add/delete the data, maybe it would impact developer team's progress or others. So we just use the custom one or take a backup.

    If I have misunderstood this issue, feel free to let me know.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 24, 2018 9:52 AM
    Moderator
  • Thanks for your feedback.

    I raise that topic because some says that a Unit test shouold not hit a database but this is not the case becasue for me a Unittest can hit a databse when you need to deal with a complete return data set and validate the whole loop earlier before integration. If you wait for integration in my mind is too late.

    This is for that reason I was trying to get some feeling about others having a duplicated databse whish can serve has test.

    But then as a developper if I have a visual stuio project with UnitTest, should I have an other Unit Test project which gets data from my test db too ?

    regards


    Your knowledge is enhanced by that of others.

    Thursday, October 25, 2018 6:58 AM
  • Thanks for your friendly response, it seems that you use another account to reply this case:) am I right?

    About whether we should use the same DB during developing and testing, it is not really strict. Maybe different companies or teams have their own behaviors or requirements.

    >>But then as a developper if I have a visual stuio project with UnitTest, should I have an other Unit Test project which gets data from my test db too ?

    So for this issue, it is also opening, but we could have the measurement criteria and make sure that whether it is convenient or impact other test projects or whether we really have to use the DB or others.

    Sincerely,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by wakefun Wednesday, October 31, 2018 10:51 AM
    Friday, October 26, 2018 2:42 AM
    Moderator
  • Hi serge,

    Do you get helpful suggestions for this issue? Any latest information, feel free to share it here.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by wakefun Wednesday, October 31, 2018 10:51 AM
    Tuesday, October 30, 2018 2:00 AM
    Moderator