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.
This is the Repository
- public class ProductRepository : IProductRepository
- {
- 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();
- }
- }
- }