public void Behavior_When_Category_Is_Null() { // arrange var cat = new Category { id = 1, name = "caramel" }; var sub = new Subject { id = 1, name = "Twix", category_id = 1 }; Mock <IDbContext> mock = new Mock <IDbContext>(); mock.Setup(m => m.Categories.Add(cat)) .Returns((Category u) => u); mock.Setup(m => m.Subjects.Add(sub)) .Returns((Subject u) => u); NavRepository <Category> repository = new NavRepository <Category>(mock.Object); string category = null; // act var result = repository.GetMenuItems(category); }
public void Can_Create_Categories() { // arrange var cat = new List <Category> { new Category { id = 1, name = "caramel" }, new Category { id = 2, name = "chocolate" }, }.AsQueryable(); var sub = new List <Subject> { new Subject { id = 1, name = "Twix", category_id = 1 }, new Subject { id = 2, name = "Lion", category_id = 1 }, new Subject { id = 3, name = "Smarties", category_id = 2 }, new Subject { id = 4, name = "Snickers", category_id = 1 }, new Subject { id = 5, name = "Bounty", category_id = 2 }, }.AsQueryable(); var categoriesMock = new Mock <IDbSet <Category> >(); categoriesMock.As <IQueryable <Category> >().Setup(m => m.Provider).Returns(cat.Provider); categoriesMock.As <IQueryable <Category> >().Setup(m => m.Expression).Returns(cat.Expression); categoriesMock.As <IQueryable <Category> >().Setup(m => m.ElementType).Returns(cat.ElementType); categoriesMock.As <IQueryable <Category> >().Setup(m => m.GetEnumerator()).Returns(cat.GetEnumerator()); var subjectsMock = new Mock <IDbSet <Subject> >(); subjectsMock.As <IQueryable <Subject> >().Setup(m => m.Provider).Returns(sub.Provider); subjectsMock.As <IQueryable <Subject> >().Setup(m => m.Expression).Returns(sub.Expression); subjectsMock.As <IQueryable <Subject> >().Setup(m => m.ElementType).Returns(sub.ElementType); subjectsMock.As <IQueryable <Subject> >().Setup(m => m.GetEnumerator()).Returns(sub.GetEnumerator()); Mock <IDbContext> mock = new Mock <IDbContext>(); mock.Setup(m => m.Categories) .Returns(categoriesMock.Object); mock.Setup(m => m.Subjects) .Returns(subjectsMock.Object); NavRepository <Category> repository = new NavRepository <Category>(mock.Object); // act var results = repository.GetMenuItems("caramel"); // assert Assert.AreEqual(results.Count, 4); Assert.AreEqual(results[0].Name, "caramel"); Assert.AreEqual(results[1].Name, "Twix"); Assert.AreEqual(results[2].Name, "Lion"); Assert.AreEqual(results[3].Name, "Snickers"); }