2

I have a model like this:

 public class Products
{


    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Product_D { get; set; }

    [Required(ErrorMessage="Product is required")]
    [Display(Name="Product Name")]
    public string Product_Name { get; set; }

    [Required(ErrorMessage="Photo of the product is required")]
    [Display(Name="Product Photo")]
    public byte[] Product_Photo { get; set; }

    [NotMapped]
    public HttpPostedFileBase Image { get; set; }

    [Required(ErrorMessage="Enter the Quantity Available")]
    [Display(Name = "Product Quantity")]
    public int Product_Quantity { get; set; }

    [Required(ErrorMessage="Enter the price of the product")]
    [Display(Name = "Product Price")]
    public int Product_Price { get; set; }

    [Required(ErrorMessage = "Choose your category")]
    public string Category { get; set; }


    [NotMapped]
    public List<System.Web.Mvc.SelectListItem> CategoryList { get; set; }

    [Required(ErrorMessage = "Choose your category")]
    public string Gender { get; set; }



    [NotMapped]
    public List<System.Web.Mvc.SelectListItem> GenderList { get; set; }

}

I'm trying to Mock database and i'm adding products to my mock database,I was able to provide values for all other data types and I'm not sure how to play around with Files.

 public void MockRepo()
    {
        IList<Products> products = new List<Products>
        {
            new Products { Product_D = 1, Product_Name = "C# Unleashed",Product_Quantity =  20, Product_Price = 20 },
                new Products { Product_D = 2, Product_Name = "ASP.Net Unleashed",Product_Quantity = 20, Product_Price = 30 },
                new Products { Product_D = 3, Product_Name = "Silverlight Unleashed",Product_Quantity = 20, Product_Price = 100 }
        };
        //Mock Products using MOQ
        Mock<IProduct> mockproductrepo = new Mock<IProduct>();
        //return all products
        mockproductrepo.Setup(r => r.FindAll()).Returns(products);
        // returns products by name
        mockproductrepo.Setup(r => r.FindByName(It.IsAny<string>())).Returns(products.Single());
        //returns products by id
        mockproductrepo.Setup(r => r.FindById(It.IsAny<int>())).Returns(products.Single());
    }

I don't think a providing the path of the image file would help me. Using File system would make my Test slow.Even If I do that, I should convert the image into Bytes . How can I deal with this ? Thanks in advance

sujay kodamala
  • 317
  • 1
  • 5
  • 17

1 Answers1

1

Here's an article that shows how to use Moq uploaded files

http://blog.csainty.com/2009/01/aspnet-mvc-unit-test-file-upload-with.html

as well as this this stack overflow question. Unit Test a file upload, how?

I think you are having issues testing this because are co-mingling your view model and your entity/db model. Your entity information is related to your entity model, while other information uploaded image and select lists are related to your controller context.

I'd split them apart, since it looks like the only reason you have [NotMapped] on properties is to discern a UI concern from the Db.

Community
  • 1
  • 1
Fran
  • 6,440
  • 1
  • 23
  • 35