How should I be mocking this simple service level method?

I have the following simple method at my service level. I'm not sure how I should mock different chains starting from the repository?

public ICollection<GameFile> FindAllActiveGamesFiles()
{
    return _gameFileRepository // <- this is an IRepository<GameFile>
        .Find() // <-- returns an IQueryable<T> .. in this case, an IQueryable<GameFile>
        .WhereIsActive() // <-- a simple Pipe/Filter; 'where x.IsActive'
        .ToList();
}

My preference is to use Moq, but I am very happy to see other implications. Mostly because I'm not after an exact answer to the syntax, but with an aortic answer. I.e. U need to mock IRepository<GameFile>and configure the method ToList()only ... blah blah blah ...

What I don’t understand is that I have to mock.

Greetings :)

--- Update: clarify my question

, , FindAllActiveGamesFiles(). , , gameFileRepository ( ). , , .

.

[TestMethod]
public void MyTest()
{
    // Arrange.
    Mock<IRepository<GameFile>> mockRepository = new Mock<IRepository<GameFile>>();
    mockRepository.Setup(....).MoreStuffToDo(...); // <-- that what i'm unsure about.
    IGameFileService = new GameFileService(mockRepository, fakeLoggingService);

    // Act.
    var gameFiles = gameFileService.FindAllActiveGamesFiles();

    // Asserts.
    Assert.IsNotNull(gameFiles);
    CollectionAssert.AllItemsAreNotNull(gameFiles.ToArray());
    // .. and more asserts ///
    // What about expectations? eg. that ToList() was entered/called?
}
+3
2

, , Linq. , - (IRepository <GameFile> ), . , , .

ServiceLayer, .

public class ServiceLayer
{
     private readonly IRepository<GameFile> _gameRepository;

     public SerivceLayer(IRepository<GameFile> repository)
     {
         _gameRepository = repository;
     }

     public IEnumerable<GameFile> FindAllActiveGamesFiles()
     {
         return _gameRepository
                    .Find()  // method we need to mock
                    .Where( gameFile => gameFile.IsActive)
                    .ToList();
     }
}

.... (NUnit Moq)

[TestFixture]
public class ServiceLayerFixture
{
      protected IRepository<GameFile> Repository;
      protected ServiceLayer Subject;
      protected ICollection<GameFile> Results;

      [Setup]
      public void Setup()
      {
          // create our mock
          Repository = new Mock<IRepository<GameFile>>().Object;

          // initialize our test subject
          Subject = new ServiceLayer(Repository);
      }

      [Test]
      public void WhenRepositoryDoesNotContainItems_ServiceLayer_ReturnsAnEmptyCollection()
      {
         Mock.Get(Repository)
             .Setup( r => r.Find())
             .Returns( new List<GameFile>().AsQueryable() );

         Results = Subject.FindAllActiveGameFiles();

         Assert.AreEqual(0, Results.Count);
      }

      [Test]
      public void WhenRepositoryDoesNotContainActiveItems_ServiceLayer_ReturnsAnEmptyCollection()
      {
         Mock.Get(Repository)
             .Setup( r => r.Find())
             .Returns( 
                 new List<GameFile>()
                     {
                        new GameFile { IsActive = false },
                        new GameFile { IsActive = false }
                     }.AsQueryable() );

         Results = Subject.FindAllActiveGameFiles();

         Assert.AreEqual(0, Results.Count);            
      }

      [Test]
      public void WhenRepositoryContainActiveItems_ServiceLayer_FiltersItemsAppropriately()
      {
         Mock.Get(Repository)
             .Setup( r => r.Find())
             .Returns( 
                 new List<GameFile>()
                     {
                        new GameFile { IsActive = true },
                        new GameFile { IsActive = false }
                     }.AsQueryable() );

         Results = Subject.FindAllActiveGameFiles();

         Assert.AreEqual(1, Results.Count);            
      }
}

IRepository.

, :

[Test]
public void WhenTheRepositoryFails_ServiceLayer_ShouldHandleExceptionsGracefully()
{
    Mock.Get(Repository)
        .Setup( r => r.Find())
        .Throws( new InvalidOperationException() );

    Results = Subject.FindAllActiveGameFiles();

    Assert.AreEqual(0, Results.Count);          
}

, , ?

[Test]
[ExpectedException(typeof(GameFileNotFoundException))]
public void WhenTheRepositoryFails_ServiceLayer_ShouldReportCustomError()
{
    Mock.Get(Repository)
        .Setup( r => r.Find())
        .Throws( new InvalidOperationException() );

    Subject.FindAllActiveGameFiles();
}
+3

, ToList(), , Find(), WhereIsActive() ToList(). , .

. . FindAllActiveGamesFiles() , . FindAllActiveGamesFiles(), .

FindAllActiveGamesFiles() mockRepository. , Find() WhereIsActive() , .

API , psuedocode:

Mock<IRepository<GameFile>> mockRepository = new Mock<IRepository<GameFile>>();
List<GameFile> result = new List<GameFile>();
mockRepository.expect('Find')->willReturn(mockRepository);
mockRepository.expect('WhereIsActive')->willReturn(mockRepository);
mockRepository.expect('ToList')->willReturn(result);
IGameFileService service = new GameFileService(mockRepository, fakeLoggingService);

assertSame(service.FindAllActiveGamesFiles(), result);

, , . , .

0

Source: https://habr.com/ru/post/1791217/


All Articles