This is the test project
- public class UnitTest1
- {
- //private ProductRepository
repository; - //private ProductRepository
productDateRepositor; - private IProductRepository productRepository;
- private IProductDateRepository productDateRepository;
- public ProductController productController;
- List
list; - [TestInitialize]
- public void setUpController()
- {
- //repository = new ProductRepository
(); - //productDateRepositor = new ProductRepository
(); - productController = new ProductController(productRepository, productDateRepository);
- list = new List
() - {
- 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()
- {
- //Arrange
- var controller = productController;
- //Act
- var result = controller.Index("AAP") as ViewResult;
- var tickerdetails = (ProductModel)result.ViewData.Model;
- //Assert
- 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
_products; - public ProductRepository()
- {
- _products = new List
() - {
- // Add products for the Demonstration
- 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
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
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
IProductRepository.GetAll() - {
- return _products.ToList();
- }
- }
- }
Jayanth ReddyPosted Aug 20, 2019, 4:52 AM
Rajanikant HawaldarPosted Aug 18, 2019, 11:30 AM
Amit GuptaPosted Aug 18, 2019, 6:07 AM