dimanche 6 décembre 2015

Unit Testing - Can't Access Method From Repository Class

I'm writing a Unit Test, which verifies a specific product is selected with the correct data. Currently, the test is providing the following error:

System.NullReferenceException: Object reference not set to an instance of an object.

While debugging through the test, I noticed that my result variable is null... I thought I was calling my SelectProduct method correctly, but not sure what is wrong.

Additional question - Any suggestions on how to better Assert?

[TestClass]
    public class ProductRepositoryTests
    {
        [TestMethod]
        public void SelectProduct_selectProduct_productIsSelected()
        {        
            // access ProductRepository
            Mock<IProductRepository> mock = new Mock<IProductRepository>();

            // Arrange - mocked product
            Product product1 = new Product
            {
                ProductId = 1,
                ProductName = "Snicker Bar",
                ProductPrice = .25M,
                ProductCategory = "Candy",
                ProductQuantity = 12
            };

            // Act - select new product using SelectProduct method
            var result = mock.Object.SelectProduct(product1.ProductId);

            // Assert
            Assert.AreEqual(result.ProductId, 1);
            Assert.AreEqual(result.ProductName, "Snicker Bar");
            Assert.AreEqual(result.ProductPrice, .25M);
        }
    }

Here is the other code for my Repository layer:

Interface:

public interface IProductRepository
{
    Product SelectProduct(int productId);
}

Repository Class:

 public class ProductRepository : IProductRepository
    {
        public Product SelectProduct(int productId)
        {
            throw new System.NotImplementedException();
        }
    }

Aucun commentaire:

Enregistrer un commentaire