针对两个提供商进行集成测试
我是C#的新手,并尝试为服务编写集成测试。该服务使用2个提供商作为示例,我不知道如何将它们包括在我的测试中。我正在使用Xunit。
// Service
namespace App.Services
{
public interface IProvider
{ }
public class FirstProvider : IProvider
{ }
public class SecondProvider : IProvider
{ }
public class AppManager
{
private readonly IEnumerable<IProvider> _providers;
public AppManager(
IEnumerable<IProvider> providers)
{
_providers = providers;
}
public asyn Task ListItems()
{
foreach (var singleProvider in _providers) // When running test, I got ERROR here: _providers is null)
{
// do something
}
}
}
}
//测试
public class AppManagerTest
{
private readonly IEnumerable<IProvider> _providers;
[Fact]
public async ListItems()
{
// Arrange
var sut = new AppManager(_providers);
// Act
// Assert
}
}
运行测试时,我在服务代码中指向的错误时会出现错误。错误是 system.nullReferenceException:对象引用未设置为对象的实例。调试测试表明_providers是无效的。
因此,正如我所理解的那样,我的测试没有得到任何提供商。我应该在这里做什么?
I'm new to C# and trying to write an integration test for a service. The service uses 2 providers as example below, and I don't know how to include them in my test. I'm using xUnit.
// Service
namespace App.Services
{
public interface IProvider
{ }
public class FirstProvider : IProvider
{ }
public class SecondProvider : IProvider
{ }
public class AppManager
{
private readonly IEnumerable<IProvider> _providers;
public AppManager(
IEnumerable<IProvider> providers)
{
_providers = providers;
}
public asyn Task ListItems()
{
foreach (var singleProvider in _providers) // When running test, I got ERROR here: _providers is null)
{
// do something
}
}
}
}
// Test
public class AppManagerTest
{
private readonly IEnumerable<IProvider> _providers;
[Fact]
public async ListItems()
{
// Arrange
var sut = new AppManager(_providers);
// Act
// Assert
}
}
When running test, I got an error as pointed above in my service code. The error is System.NullReferenceException : Object reference not set to an instance of an object. Debugging the test shows that _providers is null.
So as I understood, my test does not get any of the providers. What should I do here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论