How I should mock below lines?
ABCRepository abcObj = new ABCRepository();
var model = new NamesList
{
ALayoutNames = abcObj.ALayout(),
BLayoutNames = abcObj.BLayout(),
CLayoutNames = abcObj.CLayout(),
DLayoutNames = abcObj.DLayout(),
ELayoutNames = abcObj.ELayout()
};
I have mocked first line with its interface like below
Mock<IABCRepository> _iabcrepository = new Mock<IABCRepository>();
But how to proceed for NamesList object?
This needs a bit of refactoring. That is why I find that Unit Tests help create a better code.
So here it goes, step by step:
public class ClassToTest {
private IABCRepository abcObj;
public ClassToTest(IABCRepository repo) {
this.abcObj = repo;
}
public DoingSomething() {
var model = new NamesList
{
ALayoutNames = this.abcObj.ALayout(),
BLayoutNames = this.abcObj.BLayout(),
CLayoutNames = this.abcObj.CLayout(),
DLayoutNames = this.abcObj.DLayout(),
ELayoutNames = this.abcObj.ELayout()
};
}
}
Mock<IABCRepository> _iabcrepository = new Mock<IABCRepository>();
_iabcrepository.Setup(foo => foo.ALayout()).Returns(MockValue);
_iabcrepository.Setup(foo => foo.BLayout()).Returns(MockValue);
// etc
var classToTest = new ClassToTest(_iabcrepository.Object);
Now when you call the DoSomething function, the mocked repository will return the mocked values.