I am beginner to Autofac. I have initially created a sample project and then replaced all of my new () keyword with constructor injection. Then replaced the constructor injection with Autofac and the project is working fine.
My issue: I have my unit test cases where I pass parameters to constructors , as I had replaced my project with Autofac. I am unsure how to replace the unit test cases constructor injection to use Autofac.
public class GenericHelper : IGenericHelper
{
private IDAL _DAL;
private IDate _Date;
//public GenericHelper(IDAL DAL,IDate Date) /* Constructor injection */
//{ _DAL = DAL;
// _Date = Date;
//}
public String CalculateMatruity(IAge AgeObj)
{
String Maturity = "", AgeConfiguration;
//IDAL dalObj = _DAL;
//IDate dateObj = _Date;
var builder = new ContainerBuilder();
builder.RegisterType<AgeDAL>().As<IDAL>();
builder.RegisterType<CurrentDate>().As<IDate>();
using (var container = builder.Build())
{
// AgeConfiguration = dalObj.GetMaturityConfiguration(); /* initial approach */
AgeConfiguration = container.Resolve<IDAL>().GetMaturityConfiguration();
//Int64 Age = dateObj.Date.Year - AgeObj.Birthdate.Year; /* initial approach */
var _d = container.Resolve<IDate>().Date.Year;
Int64 Age = _d - AgeObj.Birthdate.Year; /* Using autofac approach */
var config = AgeConfiguration.Split('|').Select(s => s.Split('=')).ToDictionary(c => c[1], c => int.Parse(c[0]));
............
My error unit tests:
[TestMethod]
public void TestGenericHelper()
{
Person age = new Person();
age.Birthdate = DateTime.Parse("1987-06-16");
String expected = "Teen";
DateTime FakeDate = Convert.ToDateTime("2000-01-01");
String fake = "0=Child|13=Teen|18=Adult";
FakeDate DateHelperObj = new FakeDate(FakeDate);
FakeAgeDAL fakeObj = new FakeAgeDAL(fake);
GenericHelper GenericHelperObj = new GenericHelper(fakeObj, DateHelperObj); /* I Need to replace it use Autofac */
String actual = GenericHelperObj.CalculateMatruity(age);
Assert.AreEqual(expected, actual);
}
I need to replace the GenericHelper class instance to use autofac but call the dummy object.Can anyone please advice.
Thanks
Aucun commentaire:
Enregistrer un commentaire