I am using 3 tier architecture: Controller, Business, and Data Layer. In my Data Layer, I am making a call to an Sql Server Database by passing Connection String and other necessary parameters.
I have to write unit tests for the Controller layer and Business layer. I want to write a stub (fake repository) from which I would return the hard coded values/result. When I write a test for business layer, the logic should call this stub instead of the real database.
How can I write the code in the business layer to achieve this?
Business Layer:
public string GetValues(string xmlData)
{
DataObject do = new DataObject ();
string result = do.GetValues(xmlData);
return result;
}
Data access:
public static string GetValues(string xmlData)
{
return SqlHelper.ExecuteScalar(
ConfigurationManager.AppSettings["ConnectionString"].ToString(),
"DBO.usp_GetDetail",
xmlData
).ToString();
}
In order to test you scenario, your code has to be testable. If it follows SOLID principles it most likely is. But let's focus on what is essential to do this kind of unit testing:
Same principles should apply to Controller - Business Layer interaction. When you stick to those two rules (which essentially narrows to SOLID's dependency inversion principle), your code will be much more unit testable than otherwise (sticking to SOLID principles is overall good idea).
Since you'll probably end up writing mocks/stubs, I suggest using existing mocking framework, like Moq or FakeItEasy.
Edit - if code is that tighly coupled, your options are limited to: