Hi, 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.