Click here to Skip to main content
15,909,591 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is the test project
C#
public class UnitTest1
    {

        //private ProductRepository<iproductrepository> repository;
        //private ProductRepository<iproductdaterepository> productDateRepositor;
        private IProductRepository productRepository;
        private IProductDateRepository productDateRepository;
        public ProductController productController;
        List<productmodel> list;

        [TestInitialize]
        public void setUpController()
        {
            //repository = new ProductRepository<iproductrepository>();
            //productDateRepositor = new ProductRepository<iproductdaterepository>();
            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()
        {
            

            //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.

What I have tried:

This is the Repository
C#
public class ProductRepository : IProductRepository,IProductDateRepository
    {
        private List<productmodel> _products;

        public ProductRepository()
        {
            _products = new List<productmodel>()
            {
                // 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<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 ProductRepository<iproductrepository> repository;
        //private ProductRepository<iproductdaterepository> productDateRepositor;
        private IProductRepository productRepository;
        private IProductDateRepository productDateRepository;
        public ProductController productController;
        List<productmodel> list;

        [TestInitialize]
        public void setUpController()
        {
            //repository = new ProductRepository<iproductrepository>();
            //productDateRepositor = new ProductRepository<iproductdaterepository>();
            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()
        {
            

            //Arrange
            var controller = productController;
            //Act
            
            var result = controller.Index("AAP") as ViewResult;
            var tickerdetails = (ProductModel)result.ViewData.Model;

            //Assert
            Assert.AreEqual("AAP",tickerdetails.tickersymbol );
            
        }
Posted
Updated 22-Aug-19 3:40am
v4
Comments
Patrice T 17-Aug-19 23:04pm    
Describe 'not working'
OriginalGriff 18-Aug-19 1:47am    
"It's not working" is one of the most useless problem descriptions we get: it tells us absolutely nothing about the problem. We don't know if you get an error message, or the wrong data, or even that that code compiles successfully!
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
So tell us what happens when you run that code, what you expected to happen, how you checked what happened. Help us to help you!
Use the "Improve question" widget to edit your question and provide better information.

 
Share this answer
 
Quote:
C#
//repository = new ProductRepository<iproductrepository>();
//productDateRepositor = new ProductRepository<iproductdaterepository>();
productController = new ProductController(productRepository, productDateRepository);
The most likely cause is that you've commented out the lines that create the product repository and product date repository. As a result, those two fields are null when you pass them in to the ProductController constructor. That constructor doesn't validate its arguments, and the class later tries to access a member of one of those repositories.

Initialize the fields before constructing the controller:
C#
var productRepository = new ProductRepository();
repository = productRepository;
productDateRepositor = productRepository;
Also, add argument validation to your controller's constructor:
C#
public ProductController(IProductRepository repository, IProductDateRepository productDateRepositor)
{
    if (repository is null) throw new ArgumentNullException(nameof(repository));
    if (productDateRepositor is null) throw new ArgumentNullException(nameof(productDateRepositor));
    
    _repository = repository;
    _productDateRepository = productDateRepositor;
}

As a side-note, it seems odd that you've chosen to split the repository into two interfaces, but leave it as a single class implementing both interfaces.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900