public void UpdateAllChildItemValuesTest() { // Arrange: set up a context mock with an initial collection of child items (obtained from mocked root items), exposed through a provider mock. Mock <IMyContext> myContextMock = new Mock <IMyContext>(); var myChildItems = new DbSetMock <MyChildItem>(MyMockedRootItems.SelectMany(r => r.MyChildItems)); myContextMock.Setup(m => m.MyChildItems).Returns(myChildItems); Mock <IMyContextProvider> myContextProvider = new Mock <IMyContextProvider>(); myContextProvider.Setup(m => m.CreateContext()).Returns(myContextMock.Object); // Act: create the service instance and call UpdateAllChildItemValues operation with a specific argument. var service = new MyService(myContextProvider.Object); const string postfix = "(postfix)"; service.UpdateAllChildItemValues(postfix); // Assert: verify that the context is created, and that the side effects are as expected. myContextProvider.Verify(m => m.CreateContext()); Assert.IsTrue(myChildItems.All(c => c.MyChildValue.EndsWith($" {postfix}"))); }
public void RemoveItemsTest() { // Arrange: set up a context mock with initial collections of root items and child items, exposed through a provider mock. Mock <IMyContext> myContextMock = new Mock <IMyContext>(); var myRootItems = new DbSetMock <MyRootItem>(MyMockedRootItems); myContextMock.Setup(m => m.MyRootItems).Returns(myRootItems); var myChildItems = new DbSetMock <MyChildItem>(MyMockedRootItems.SelectMany(r => r.MyChildItems)); myContextMock.Setup(m => m.MyChildItems).Returns(myChildItems); Mock <IMyContextProvider> myContextProvider = new Mock <IMyContextProvider>(); myContextProvider.Setup(m => m.CreateContext()).Returns(myContextMock.Object); // Act: create the service instance and call RemoveItems operation. var service = new MyService(myContextProvider.Object); service.RemoveItems(); // Assert: verify that the context is created, and that the side effects are as expected. myContextProvider.Verify(m => m.CreateContext()); Assert.AreEqual(0, myRootItems.Count()); }