public void ShouldFakeAddingNewEntityToContext() { var dinner = new Dinner { DinnerID = 1 }; IList <Dinner> dinners = new List <Dinner>(); // ARRANGE NerdDinners nerdDinners = new NerdDinners(); // Arranging: When nerdDinners.Dinners.Add() is called with a Dinner argument, that equals dinner, it should add that argument // to the dinners collection instead. Mock.Arrange(() => nerdDinners.Dinners.Add(dinner)).DoInstead((Dinner d) => dinners.Add(d)); // Then we arrange that SaveChanges() should do nothing for the nerdDinners instance. Mock.Arrange(() => nerdDinners.SaveChanges()).DoNothing(); // ACT nerdDinners.Dinners.Add(dinner); nerdDinners.SaveChanges(); // ASSERT Assert.AreEqual(1, dinners.Count); Assert.AreEqual(1, dinners[0].DinnerID); }
public string GetAndFormatDinnerInfo(int dinnerID) { StringBuilder info = new StringBuilder(); using (var dbContext = new NerdDinners()) { Dinner checkedDinner = (from d in dbContext.Dinners where d.DinnerID == dinnerID select d).FirstOrDefault(); info.AppendFormat("Title: {0} \n", checkedDinner.Title); info.AppendFormat("DinnerID: {0} \n", checkedDinner.DinnerID); info.AppendFormat("EventDate: {0} \n", checkedDinner.EventDate.Date); info.AppendFormat("Address: {0} \n", checkedDinner.Address); info.AppendFormat("HostedBy: {0}", checkedDinner.HostedBy); } return(info.ToString()); }
public void ShouldReturnFakeCollectionWhenExpected() { // ARRANGE NerdDinners nerdDinners = new NerdDinners(); // Arranging: When nerdDinners.Dinners_GET is called, it should return fake collection (fakeDinners). Mock.Arrange(() => nerdDinners.Dinners).ReturnsCollection(FakeDinners()); // ACT - We call the nerdDinners.Dinners and search for a dinner with a certain DinnerID. var actualQuery = from d in nerdDinners.Dinners where d.DinnerID == 1 select d; // ASSERT // We assert that the nerdDinners.Dinners collection will actually return a collection with 2 items. Assert.AreEqual(3, nerdDinners.Dinners.Count()); // We assert that there is only one item in our collection and this item has DinnerID equal to one. Assert.AreEqual(1, actualQuery.Count()); Assert.AreEqual(1, actualQuery.First().DinnerID); }
public void ShouldReturnFakeCollectionForFutureInstance() { // ARRANGE NerdDinners nerdDinners = new NerdDinners(); // Arranging: When nerdDinners.Dinners_GET is called, it should return fake collection (fakeDinners) no matter the instance. Mock.Arrange(() => nerdDinners.Dinners).IgnoreInstance().ReturnsCollection(FakeDinners()); // ACT // We will create another instance of the NerdDinners class and will act on it. NerdDinners actualNerdDinners = new NerdDinners(); var actualQuery = from d in actualNerdDinners.Dinners where d.DinnerID == 1 select d; // ASSERT // We assert that the actualNerdDinners.Dinners collection will actually return a collection with 2 items. Assert.AreEqual(3, actualNerdDinners.Dinners.Count()); // We assert that there is only one item in our collection and this item has DinnerID equal to one. Assert.AreEqual(1, actualQuery.Count()); Assert.AreEqual(1, actualQuery.First().DinnerID); }