模拟存储库方法返回 null 而不是预期值

huangapple go评论81阅读模式
英文:

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&lt;IUnitOfWork&gt;();
   
    unitOfWorkMock.Setup(x =&gt; x.CarRepository.GetById(It.IsAny&lt;int&gt;()))
    .ReturnsAsync(new Car(){Name = &quot;test&quot;});
	
	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&lt;Car, int&gt;
{
}

public interface IRepository&lt;T, TId&gt; where T : Entity&lt;TId&gt;
{
   Task&lt;T?&gt; GetById(TId id);
}
public class Car : Entity&lt;int&gt;
{   
}

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。car2Test() 方法的底部是符合预期的。

所以提问者必须在问题中提供更多细节。尝试准备一个完整的重现代码(没有隐式类型)。

英文:

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&lt;Car, int&gt;
{
}
public interface IRepository&lt;T, TId&gt; where T : Entity&lt;TId&gt;
{
    Task&lt;T?&gt; GetById(TId id);                                  // question mark on T? is allowed; T is a reference type; nullable reference types are enabled
}
public class Car : Entity&lt;int&gt;
{
    internal string? Name { get; init; }
}
public class Entity&lt;T&gt;
{
}

public class F
{
    [Test]
    public async Task Test()
    {
        var unitOfWorkMock = new Mock&lt;IUnitOfWork&gt;();          // also works: new Mock&lt;IUnitOfWork&gt;(MockBehavior.Strict)

        unitOfWorkMock.Setup(x =&gt; x.CarRepository.GetById(It.IsAny&lt;int&gt;()))
            .ReturnsAsync(new Car { Name = &quot;test&quot;, });

        Car? car2 = await unitOfWorkMock.Object.CarRepository.GetById(1);

        // car2 is nonnull and instance has &#39;Name&#39; equal to &quot;test&quot;
    }
}

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).

huangapple
  • 本文由 发表于 2023年2月26日 19:16:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定