英文:
Mocking repository method returns null instead expected value
问题
public interface IUnitOfWork
{
ICarRepository CarRepository { get; }
...
}
ICarRepository has (besides other methods) GetById(int id) method.
That call I want to mock inside IUnitOfWork mock.
[Test]
public async Task Test()
{
var unitOfWorkMock = new Mock<IUnitOfWork>();
unitOfWorkMock.Setup(x => x.CarRepository.GetById(It.IsAny<int>()))
.ReturnsAsync(new Car() { Name = "test" });
var car = unitOfWork.Object.CarRepository.GetById(1);
//returns null
var car2 = await unitOfWork.Object.CarRepository.GetById(1);
//returns null
Any thoughts?
}
UPDATE:
public interface ICarRepository : IRepository<Car, int>
{
}
public interface IRepository<T, TId> where T : Entity<TId>
{
Task<T?> GetById(TId id);
}
public class Car : Entity<int>
{
}
I'm expecting that car should be populated with whatever I passed in Returns statement in the mock setup but instead I'm getting null.
英文:
public interface IUnitOfWork
{
ICarRepository CarRepository { get; }
...
}
ICarRepository has (besides other methods) GetById(int id) method.
That call I want to mock inside IUnitOfWork mock.
[Test]
public async Task Test()
{
var unitOfWorkMock = new Mock<IUnitOfWork>();
unitOfWorkMock.Setup(x => x.CarRepository.GetById(It.IsAny<int>()))
.ReturnsAsync(new Car(){Name = "test"});
var car = unitOfWork.Object.CarRepository.GetById(1);
//returns null
var car2 = await unitOfWork.Object.CarRepository.GetById(1);
//returns null
Any thoughts?
}
UPDATE:
public interface ICarRepository : IRepository<Car, int>
{
}
public interface IRepository<T, TId> where T : Entity<TId>
{
Task<T?> GetById(TId id);
}
public class Car : Entity<int>
{
}
I'm expecting that car should be populated with whatever I passed in Returns statement in the mock setup but instead I'm getting null.
答案1
得分: 1
这不是一个实际答案,只是一个详细的评论,报告这段完整的代码没有重现问题,使用Moq版本4.18.4。car2 在 Test() 方法的底部是符合预期的。
所以提问者必须在问题中提供更多细节。尝试准备一个完整的重现代码(没有隐式类型)。
英文:
This is not an actual answer; just a lengthy comment to report that this complete code:
public interface IUnitOfWork
{
ICarRepository CarRepository { get; }
}
public interface ICarRepository : IRepository<Car, int>
{
}
public interface IRepository<T, TId> where T : Entity<TId>
{
Task<T?> GetById(TId id); // question mark on T? is allowed; T is a reference type; nullable reference types are enabled
}
public class Car : Entity<int>
{
internal string? Name { get; init; }
}
public class Entity<T>
{
}
public class F
{
[Test]
public async Task Test()
{
var unitOfWorkMock = new Mock<IUnitOfWork>(); // also works: new Mock<IUnitOfWork>(MockBehavior.Strict)
unitOfWorkMock.Setup(x => x.CarRepository.GetById(It.IsAny<int>()))
.ReturnsAsync(new Car { Name = "test", });
Car? car2 = await unitOfWorkMock.Object.CarRepository.GetById(1);
// car2 is nonnull and instance has 'Name' equal to "test"
}
}
together with Moq version 4.18.4, does not reproduce the problem. car2 is as expected at the bottom of Test().
So the asker must supply more details in the question. Try to prepare a complete reproduction code (with no types implicit).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论