首页 > 解决方案 > Xunit测试中“不支持的表达式”是什么意思?

问题描述

我尝试在 .net core3 中编写我的第一个 xUnit 测试。我创建了一个 Repository 接口和存储库的类实现,所有存储库都从它派生。我创建了一个测试项目,在其中尝试测试已实现的存储库。

这是存储库测试类:

public class RepositoriesTests
{
    private Mock<InternalDataRepository> _repositoryInternalMock;

    public RepositoriesTests()
    {
        //Mock immitates functiuonality of the object
        _repositoryInternalMock = new Mock<InternalDataRepository>();
    }

    [Fact]
    public void ShouldReturnPhoneNum() {

            _repositoryInternalMock.Setup(x => x.GetAllAsync());
    }
}

这是 InternalDataRepository 的定义:

public class InternalDataRepository : Repository<Internal>, IInternalDataRepository
{

}

和存储库接口:

public interface IRepository<T> where T : class
{
    Task<T> GetAsync(int id);
    Task<IEnumerable<T>> GetAllAsync();
    Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate);

    Task AddAsync(T entity);
    Task AddRangeAsync(IEnumerable<T> entities);

    void Remove(T entity);
    void RemoveRange(IEnumerable<T> entities);

    Task<int> SaveAsync();
}

但是当我对这一行的 RepositoriesTests 对象运行测试时:

_repositoryInternalMock.Setup(x => x.GetAllAsync());

我收到此错误:

 System.NotSupportedException : Unsupported expression: x => x.GetAllAsync()
 

知道为什么测试失败了吗?为什么我会收到不支持的表达式错误?

标签: c#asp.net-corexunit.net

解决方案


推荐阅读