This is the test project
public class UnitTest1
{
private IProductRepository productRepository;
private IProductDateRepository productDateRepository;
public ProductController productController;
List<productmodel> list;
[TestInitialize]
public void setUpController()
{
productController = new ProductController(productRepository, productDateRepository);
list = new List<productmodel>()
{
new ProductModel(){Id=1,tickersymbol = "AAP", Price = 11100 ,Date=DateTime.Parse("12/25/2018") },
new ProductModel(){Id=2,tickersymbol = "DAL", Price = 22200,Date=DateTime.Parse("11/27/2018")},
new ProductModel(){Id=3,tickersymbol = "GE", Price = 33300,Date=DateTime.Parse("12/26/2018")},
new ProductModel(){Id=4,tickersymbol = "AMZN", Price = 44400,Date=DateTime.Parse("11/27/2018")},
new ProductModel(){Id=5,tickersymbol = "FNB", Price = 55500,Date=DateTime.Parse("12/28/2018")},
};
}
[TestMethod]
public void Index()
{
var controller = productController;
var result = controller.Index("AAP") as ViewResult;
var tickerdetails = (ProductModel)result.ViewData.Model;
Assert.AreEqual("AAP",tickerdetails.tickersymbol );
}
While running this I am getting Message: Test method UnitTestProject2.UnitTest1.Index threw exception:
System.NullReferenceException: Object reference not set to an instance of an object.
What I have tried:
This is the Repository
public class ProductRepository : IProductRepository,IProductDateRepository
{
private List<productmodel> _products;
public ProductRepository()
{
_products = new List<productmodel>()
{
new ProductModel(){Id=1,tickersymbol = "AAP", Price = 11100 ,Date=DateTime.Parse("12/25/2018") },
new ProductModel(){Id=2,tickersymbol = "DAL", Price = 22200,Date=DateTime.Parse("11/27/2018")},
new ProductModel(){Id=3,tickersymbol = "GE", Price = 33300,Date=DateTime.Parse("12/26/2018")},
new ProductModel(){Id=4,tickersymbol = "AMZN", Price = 44400,Date=DateTime.Parse("11/27/2018")},
new ProductModel(){Id=5,tickersymbol = "FNB", Price = 55500,Date=DateTime.Parse("12/28/2018")},
};
}
public IEnumerable<productmodel> GetPrice(string Name, DateTime? Date)
{
if (Date != null && Name!="")
{
var product = _products.Where(x => x.tickersymbol.StartsWith(Name) && x.Date == Date).ToList();
return product;
}
else
{
return _products.ToList();
}
}
ProductModel IProductRepository.Get(int id)
{
return _products.Find(p => p.Id == id);
}
IEnumerable<productmodel> IProductRepository.GetAll(string Name)
{
if (Name != "")
{
var product = _products.Where(x => x.tickersymbol.StartsWith(Name) || Name == null).ToList();
return product;
}
else
{
return _products.ToList();
}
}
IEnumerable<productmodel> IProductRepository.GetAll()
{
return _products.ToList();
}
}
Controller
public class ProductController : Controller
{
private readonly IProductRepository _repository;
private readonly IProductDateRepository _productDateRepository;
public ProductController(IProductRepository repository, IProductDateRepository productDateRepositor)
{
_repository = repository;
_productDateRepository = productDateRepositor;
}
public ActionResult Index()
{
var model = _repository.GetAll();
return View(model);
}
[HttpPost]
public ActionResult Index(string Name)
{
var model = _repository.GetAll(Name);
return View(model);
}
public ActionResult Details(string Name, DateTime? Date)
{
var model = _productDateRepository.GetPrice(Name, Date);
return View(model);
}
Unit test file
<pre>[TestClass]
public class UnitTest1
{
private IProductRepository productRepository;
private IProductDateRepository productDateRepository;
public ProductController productController;
List<productmodel> list;
[TestInitialize]
public void setUpController()
{
productController = new ProductController(productRepository, productDateRepository);
list = new List<productmodel>()
{
new ProductModel(){Id=1,tickersymbol = "AAP", Price = 11100 ,Date=DateTime.Parse("12/25/2018") },
new ProductModel(){Id=2,tickersymbol = "DAL", Price = 22200,Date=DateTime.Parse("11/27/2018")},
new ProductModel(){Id=3,tickersymbol = "GE", Price = 33300,Date=DateTime.Parse("12/26/2018")},
new ProductModel(){Id=4,tickersymbol = "AMZN", Price = 44400,Date=DateTime.Parse("11/27/2018")},
new ProductModel(){Id=5,tickersymbol = "FNB", Price = 55500,Date=DateTime.Parse("12/28/2018")},
};
}
[TestMethod]
public void Index()
{
var controller = productController;
var result = controller.Index("AAP") as ViewResult;
var tickerdetails = (ProductModel)result.ViewData.Model;
Assert.AreEqual("AAP",tickerdetails.tickersymbol );
}