c# - 模拟函数返回空值,而不是给出模拟值
问题描述
我想在以下功能上编写测试用例
public async Task<List<Author>> GetAuthor()
{
using IDbConnection db = GetDbconnection();
var result = await Task.FromResult(dapperWrapper.GetAll<Author>(db, $"SELECT TOP (10) liAuthorId as Id, sFirstName as FirstName, sLastName as LastName from dbo.Authors", null, commandType: CommandType.Text));
return result;
}
我试图在 GetAuthor 方法中模拟 GetAll 方法并传递模拟值以返回该函数,但是当我尝试运行测试方法 GetAuthor 时,GetAll 方法给出空值,尽管在返回函数中给出了模拟值。
public async Task TestMethodTestAsync()
{
var expectedConnectionString = "url";
using IDbConnection db = new SqlConnection(expectedConnectionString);
var expectedQuery = $"SELECT TOP (10) liAuthorId as Id, sFirstName as FirstName, sLastName as LastName from dbo.Authors";
List<Author> author =
new List<Author> {
new Author { Id = 1, FirstName = "abc", LastName = "def" },
new Author { Id = 1, FirstName = "abc", LastName = "def" },
new Author { Id = 1, FirstName = "abc", LastName = "def" },
new Author { Id = 1, FirstName = "abc", LastName = "def" },
new Author { Id = 1, FirstName = "abc", LastName = "def" },
new Author { Id = 1, FirstName = "abc", LastName = "def" },
new Author { Id = 1, FirstName = "abc", LastName = "def" },
new Author { Id = 1, FirstName = "abc", LastName = "def" },
new Author { Id = 1, FirstName = "abc", LastName = "def" },
new Author { Id = 1, FirstName = "abc", LastName = "def" }
};
mockDapperWrapper.Setup(x => x.GetAll<Author>(db, expectedQuery, null, CommandType.Text)).Returns(author);
var result = newsRepository.GetAuthor();
作者模型
public class Author : BaseModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
因为我刚开始学习 C# 和新手,如果我做错了,请告诉我。
我想在以下功能上编写测试用例
public async Task<List<NewsArticle>> GetNewsArticle()
{
// Create SQL Connection
using IDbConnection db = GetDbconnection();
// Create Dapper dynamic parameter set
DynamicParameters parameter = new DynamicParameters();
// Output Paramaters
parameter.Add("@ReturnValue", dbType: DbType.Int32, direction: ParameterDirection.Output);
parameter.Add("@Message", dbType: DbType.String, direction: ParameterDirection.Output, size: 400);
var result = await Task.FromResult(dapperWrapper.GetAll<NewsArticle>(db, $"[KafkaPublisher].[GetNewsArticle]", parameter, commandType: CommandType.StoredProcedure));
return result;
}
我试图在 GetNewsArticle 方法中模拟 GetAll 方法并传递模拟值以返回该函数,但是当我尝试运行测试方法 GetNewsArticle 时,GetAll 方法给出空值,尽管在返回函数中给出了模拟值。
public async Task Should_Fetch_NewsArticle_Data()
{
DynamicParameters parameter = new DynamicParameters();
// Output Paramaters
parameter.Add("@ReturnValue", dbType: DbType.Int32, direction: ParameterDirection.Output);
parameter.Add("@Message", dbType: DbType.String, direction: ParameterDirection.Output, size: 400);
string expectedQuery = "[KafkaPublisher].[GetNewsArticle]";
List<NewsArticle> article =
new List<NewsArticle> {
new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1
};
mockDapperWrapper.Setup(x => x.GetAll<NewsArticle>(It.IsAny<IDbConnection>(), expectedQuery, null, CommandType.Text)).Returns(article);
var result = await newsRepository.GetNewsArticle();
解决方案
只有当所有参数都匹配时,Mock 才会返回配置值。传递给模拟设置的db
值与在GetAuthor
. 用于It.IsAny<IDbConnection>()
模拟设置以匹配此类型的任何参数。
推荐阅读
- swift - 无法更新数组元素
- c# - 从 C# 调用 C dll 时出现 AccessViolationException
- jakarta-mail - java Mail Search Term 对邮件号码项不起作用
- html - 如何使用谷歌翻译 TTS 说货币金额?
- node.js - 为什么我们在 webpack 中设置 node.fs: 'empty'?
- java - Java 中 Ed25519 密钥对的 PEM 文件格式
- flutter - 每次我打开 Flutter Drawer 时都会重建它
- dfa - 可构建的 DFA 数量
- sorting - Primefaces 8 上的 TableState.getMultiSortMeta()
- javascript - 错误:测试邮递员时出了点问题