gpt4 book ai didi

.net - 我应该如何模拟这个简单的服务层方法?

转载 作者:行者123 更新时间:2023-11-28 19:58:30 24 4
gpt4 key购买 nike

我的服务层中有以下简单方法。我不确定应该如何模拟不同的链接部分,从存储库开始?

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();
}

我更喜欢使用最小起订量,但我很高兴看到其他暗示.. 主要是因为我不是在寻找确切的语法答案,而是在寻找理论答案。 IE。你需要模拟 IRepository<GameFile>并设置 ToList()只有方法..等等等等......

这就是我不明白我应该 mock 的东西。

干杯:)

---更新:澄清我的问题

我要测试的是“FindAllActiveGamesFiles()”方法是否有效。因此,我相信我需要模拟 gameFileRepository(这是一个接口(interface))。我不确定这是否是我应该在此示例中模拟的唯一内容。

例如。

[TestMethod]
public void MyTest()
{
// Arrange.
Mock<IRepository<GameFile>> mockRepository = new Mock<IRepository<GameFile>>();
mockRepository.Setup(....).MoreStuffToDo(...); // <-- that's 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?
}

最佳答案

听起来您想编写一个测试来演示您的 Linq 语句的覆盖率。您已经指出您的存储库是一个接口(interface) (IRepository ) 并且应该被模拟。您只需要一些示例来说明您的服务层如何正确过滤存储库的内容。

这是我理解的 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();
}

关于.net - 我应该如何模拟这个简单的服务层方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4957718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com