I have a controller. From where I am calling(DI of BLL) to the Business Logic layer(BLL) . From BLL I am calling the Data access layer(DAL) through one more interface.
DAL layer constructor is injected with the DBContext instance( there is no Interface injected here).
So by using MOQ we can test controller , BLL & intermediated layer. But how to test the DLL layer?
public class DAL():IDAL
{
private DBEntities entity;
public DAL(DBEntities DB)
{
entity = DB;
}
public list<string> ABC()
{
var a = SqlMapper.Query<class>(entity.Database.Connection, "",param,commandType: CommandType.StoredProcedure).ToList();
return a;
}
}
Basically my query how to & what to mock here? Here we implement the IDAL, but we don't inject it here.
The question is still not 100% clear to me but I try to give an answer.
There is not need to mock DBEntities
. If you want to make sure your queries return the correct result then test it with a well known, prefilled database which you only use for your tests.
If you want to test your business logic and need to mock your data access layer, then mocking is helpfull! So create an Interface with your methods:
public interface IDAL
{
List<string> ABC();
}
In your unit test you can mock the interface with:
var dataAccessMock = new Mock<IDAL>();
dataAccessMock.Setup(x => x.ABC()).Returns(new List<string> {"ABC", "DEF", "GHI" });
// get an IDAL instance which you can inject / use
var mockedInstance = dataAccessMock.Object;
// get the mocked list
var list = mockedInstance.ABC();