Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two sp's in database. I have tested two sp's by using the MS Unit test. In deployment i faced a problem. I could not be able to access database in application server. That's why i came up with the moq framework. I need to test those two sp's by using the moq objects instead of actual database calls. Can you please suggest me is there any better way to do?

What I have tried:

I have tried like this. Just i have created one interface(moq repository) and added two methods.


public interface IImintMSNRepository
{
    bool InsertBulkImportPortfolios(DataTable mSNBulkImportPortfolioTVP,string updatedBy);
    bool InsertBulkImportIndexes(DataTable mSNBulkImportIndexTVP, string updatedBy);

}




I have tested those two methods by using moq like below.

public readonly IImintMSNRepository MockMSNRepository;

public ImintMSNUnitTests()
{

    Mock<IImintMSNRepository> mockRepo = new Mock<IImintMSNRepository>();
    mockRepo.Setup(a => a.InsertBulkImportPortfolios(It.IsAny<DataTable>(), It.IsAny<string>())).Returns(true);

    mockRepo.Setup(b => b.InsertBulkImportIndexes(It.IsAny<DataTable>(), It.IsAny<string>())).Returns(true);


    this.MockMSNRepository = mockRepo.Object;
}


[TestMethod]
public void TestPortfolioSPByMOQ()
{
    MSNBulkImportPortfolioTVP ptvp = new MSNBulkImportPortfolioTVP();

    DataRow tvprow = ptvp.NewRow();

    tvprow["PortfolioCode"] = "AutomationMoq1";
    tvprow["PortfolioName"] = "AutomationMoqName1";
    tvprow["ClientName"] = "Thomson Reuters";
    tvprow["RIC"] = "IBM.N";
    tvprow["CalculationMethodology"] = "TRGE";
    tvprow["CalendarEventCode"] = "US";
    tvprow["IsProformaPortfolio"] = 1;
    tvprow["IsParentPortfolio"] = 1;
    tvprow["IsGenerateGroupFragment"] = 1;
    tvprow["IsPushLastTick"] = 1;

    ptvp.Rows.Add(tvprow);

    bool expected = true;
    bool actual;

    actual = this.MockMSNRepository.InsertBulkImportPortfolios(ptvp, "MSNMoqUnitTestProcess");
    Assert.AreEqual(expected, actual);

}
Posted
Updated 4-Sep-18 2:21am
v2

1 solution

You don't test stored procedures via unit tests, unit tests are for testing your code logic so you use mocking frameworks to substitute the things you don't want to do (such as access a database) in a way that allows your business logic to still be tested. For example if you had a banking app that had a "GetBalance" method you would mock that method to return hard-coded values that you can use to test your logic, so "100" for testing transfers, "-100" for testing overdrafts etc.

If you look at the code you have...what logic are you actually testing? None really. You're creating a datatable and you're calling your mocked method with that table and that mocked method is returning "true". The only thing you're testing is that creating a DataTable doesn't throw an exception.

Basically you need to forget about unit testing stored procedures, that's not what unit tests are for. Mocking frameworks are for bypassing stored procs, not testing them.
 
Share this answer
 
Comments
Sree(Member 12916318) 4-Sep-18 6:22am    
Thanks for the answer. I have an idea. But, I am new to this topic. That's why I want to clarify in a more elaborate way. You mean, It does not possible to test the SP via mocking testing objects right?
F-ES Sitecore 4-Sep-18 6:31am    
You can still call the SP via your unit tests if you want to, not the mocking framework, but you shouldn't, it's not what they're for and you run into the issues you've found regarding configuration, access to resources etc. A suite of unit tests is something that should be very quick to run and tests the logic only, not the external resources, and your build server should be able to run these tests also quickly and on any environment. When you start to access external resources like databases, file systems, APIs etc via your unit tests things get much more complicated, so mocking frameworks are to bypass that kind of access.
Sree(Member 12916318) 4-Sep-18 6:44am    
Thanks for your valuable suggestion.
Sree(Member 12916318) 6-Sep-18 8:09am    
Thank you for the suggestion. I understood what you have explained to me. I had a requirement like that. Is there any possibility to test the Stored Procedures by using the moq instead of actual database calls other than MS Unit test like NUnit test. Please suggest me.
F-ES Sitecore 6-Sep-18 9:54am    
What do you mean by "test the storedprocedure"? The only way you're going to do that is to actually call the database. You use moq to mock the results of a SP call so it looks like you called the SP but you didn't really, you got a hard-coded result back.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900