c# - 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()
知道为什么测试失败了吗?为什么我会收到不支持的表达式错误?
解决方案
推荐阅读
- apache-spark - 我们可以为所有作业/应用程序使用相同的 Spark Config 吗?
- node.js - 无法使用 nestjs-joi lib 对 mongoose 模式进行验证
- python - 列表中的 Python 算法
- c++ - 如何在 C++ 中捕获异常?
- json - 无法使用 VBA 从网页解析 JSON 中的某个字段
- r - 如何使用 hist() 将统计信息放在输出图上?
- html - 在 CSS 中分配多个分区 (DIV) 并链接到 HTML
- jquery - Alternative Touch Events for Mouse drag function?
- python - 如何在 Python VLC MediaPlayer 中使用 QFile
- excel - 更改列中满足条件的每个单元格的值