C# Xunit测试不适用于mongodb服务
我想做一个小的XUnit测试,但它不起作用。(C# Xunit测试不适用于mongodb服务,c#,mongodb,unit-testing,moq,xunit,C#,Mongodb,Unit Testing,Moq,Xunit,我想做一个小的XUnit测试,但它不起作用。(AddTest正在工作,但GetAllRestaurantCounts应返回三个不工作。) 我是单元测试新手,我不知道Moq和如何使用它 我怎样才能嘲笑我的IMongoService,并获得餐厅计数 MongoService.cs public class MongoService : IMongoService { private readonly IMongoDatabase _mongoDatabase; private re
AddTest
正在工作,但GetAllRestaurantCounts应返回三个
不工作。)
我是单元测试新手,我不知道Moq和如何使用它
我怎样才能嘲笑我的IMongoService
,并获得餐厅计数
MongoService.cs
public class MongoService : IMongoService
{
private readonly IMongoDatabase _mongoDatabase;
private readonly IMongoClient _mongoClient;
public MongoService()
{
_mongoClient = new MongoClient("mongodb://localhost:27017");
_mongoDatabase = _mongoClient.GetDatabase("Restaurant");
}
public List<RestaurantDto> GetAllRestaurants()
{
var collection = _mongoDatabase.GetCollection<RestaurantDto>("Restaurant");
return collection.Find(_ => true).ToList();
}
}
公共类MongoService:IMongoService
{
私有只读IMongoDatabase_mongoDatabase;
私有只读IMongoClient_mongoClient;
公共MongoService()
{
_mongoClient=新的mongoClient(“mongodb://localhost:27017");
_mongoDatabase=\u mongoClient.GetDatabase(“餐厅”);
}
公共列表GetAllRestaurants()
{
var collection=_mongoDatabase.GetCollection(“餐厅”);
返回collection.Find(=>true).ToList();
}
}
MongoService Test.cs
public class ReviewServiceTests
{
private List<RestaurantDto> _allRestaurants = new List<RestaurantDto>()
{
new RestaurantDto() {Name="xxx", ZipCode = "111" },
new RestaurantDto() {Name="yyy", ZipCode = "222" },
new RestaurantDto() {Name="zzz", ZipCode = "333" },
};
[Fact] //Not Working
public void GetAllRestaurantsCountShouldReturnThree()
{
var _mongoService = new Mock<IMongoService>();
_mongoService.Setup(x => x.GetAll()).Returns(_allRestaurants );
var count = _mongoService.GetAll(); //GetAll() not seeing
Assert.Equal(count, 3);
}
[Fact] //Working
public void AddTest()
{
Assert.Equal(10, Add(8, 2));
}
int Add(int a, int b)
{
return a + b;
}
}
公共类ReviewServices测试
{
私人列表_allRestaurants=新列表()
{
new RestaurantTo(){Name=“xxx”,ZipCode=“111”},
new RestaurantTo(){Name=“yyy”,ZipCode=“222”},
新的RestaurantTo(){Name=“zzz”,ZipCode=“333”},
};
[事实]//不起作用
public void GetAllRestaurantCountshouldReturnThree()
{
var_mongoService=new Mock();
_mongoService.Setup(x=>x.GetAll()).Returns(_);
var count=\u mongoService.GetAll();//GetAll()看不到
断言。相等(计数,3);
}
[事实]//正在工作
public void AddTest()
{
等于(10,加上(8,2));
}
整数相加(整数a,整数b)
{
返回a+b;
}
}
您使用的最低起订量不正确
[Fact]
public void GetAllRestaurantsCountShouldReturnThree() {
var mock = new Mock<IMongoService>();
mock.Setup(x => x.GetAllRestaurants()).Returns(_allRestaurants);
IMongoService mongoService = mock.Object;
var items = mongoService.GetAllRestaurants(); //Should call mocked service;
var count = items.Count;
Assert.Equal(count, 3);
}
[事实]
public void GetAllRestaurantCountshouldReturnThree(){
var mock=new mock();
mock.Setup(x=>x.GetAllRestaurants()).Returns(_-allRestaurants);
IMongoService mongoService=mock.Object;
var items=mongoService.GetAllRestaurants();//应调用模拟服务;
变量计数=项目数;
断言。相等(计数,3);
}
了解如何在他们的工作中使用最小起订量