我有一些代码添加到我的数据库。它工作得很好,但现在我需要为它开发单元测试。我没有这方面的经验。如果你能给我推荐或建议,我会很高兴的。
testdatagenerate类(这里是用于测试的方法)
public class TestDataGenerator
{
private readonly string _dataFilePath;
private readonly Dictionary<string, List<object>> _jsonData;
public TestDataGenerator(string dataFilePath)
{
_dataFilePath = dataFilePath;
_jsonData = new Dictionary<string, List<object>>();
}
public void LoadData(string key)
{
if (!_jsonData.ContainsKey(key))
_jsonData.Add(key, JsonUtils.GetData(_dataFilePath, key).ToList());
}
public List<TEntity> GetTestData<TEntity>(string mainKey, string subKey)
{
var data = JsonConvert.DeserializeObject<List<TEntity>>(JObject.Parse(_jsonData[mainKey][0].ToString())[subKey].ToString());
return data;
}
}
与insert方法的接口
public interface ITownRepository
{
Task<Town> InsertTownName(string townName);
}
此接口的实现
public class TownRepository : ITownRepository
{
protected readonly IConnectionFactory _connectionFactory;
private readonly AppSettings _appSettings;
public TownRepository( IConnectionFactory connectionFactory
, IOptions<AppSettings> appSettings)
{
_connectionFactory = connectionFactory;
_appSettings = appSettings.Value;
}
public async Task<Town> InsertTownName(string townName)
{
using var connection = _connectionFactory.GetConnection();
var query = File.ReadAllText(_appSettings.SQlScriptsPath + "/AddTown.sql");
return await connection.QueryFirstOrDefaultAsync<Town>(query, new { townName = townName });
}
}
我有搜索查询的例子,我需要从它发展我的单元测试与插入查询它的例子与搜索
public partial class ClientRepositoryTests
{
private class TestBuilder
{
public Mock<IConnectionFactory> MockConnectionFactory { get; set; }
private readonly IOptions<AppSettings> _appSettingsOptions;
public InMemoryDatabase _memoryDatabase;
public TestDataGenerator TestDataGenerator;
private const string _jsonDataPath = @"../../../TestData/Client/ClientTestData.json";
private string _jsonDataKey;
public TestBuilder(string jsonDataKey)
{
MockConnectionFactory = new Mock<IConnectionFactory>();
_appSettingsOptions = MockSetup.GetAppSettingsOptions();
_memoryDatabase = new InMemoryDatabase();
MockConnectionFactory.Setup(c => c.GetConnection()).Returns(_memoryDatabase.OpenConnection());
TestDataGenerator = new TestDataGenerator(_jsonDataPath);
_jsonDataKey = jsonDataKey;
TestDataGenerator.LoadData(_jsonDataKey);
}
public IClientRepository Build()
{
return new ClientRepository(MockConnectionFactory.Object
, _appSettingsOptions);
}
public TestBuilder SetupClients()
{
var clients = TestDataGenerator.GetTestData<Client>(_jsonDataKey, "Clients");
_memoryDatabase.Insert(clients);
return this;
}
public TestBuilder SetupProperties()
{
var properties = TestDataGenerator.GetTestData<Client>(_jsonDataKey, "Properties");
_memoryDatabase.Insert("property_published_v", properties);
return this;
}
}
}
getclientbysearchcriteria的实现
public class ClientRepository : IClientRepository
{
protected readonly IConnectionFactory _connectionFactory;
private readonly AppSettings _appSettings;
public ClientRepository(IConnectionFactory connectionFactory
, IOptions<AppSettings> appSettings)
{
_connectionFactory = connectionFactory;
_appSettings = appSettings.Value;
}
public async Task<IList<Client>> GetClientBySearchCriteria(string searchParameter)
{
using var connection = _connectionFactory.GetConnection();
var query = File.ReadAllText(_appSettings.SQlScriptsPath + "/SearchClientAndProperties.sql");
var parameters = new { searchParameter = "%" + searchParameter + "%" };
var clients = await connection.QueryAsync<Client>(query, parameters);
return clients.ToList();
}
public async Task<Client> GetClient(int clientId)
{
string query = "Select ClientName From client Where ClientId=@ClientId";
var parameters = new { ClientId = clientId };
using (var connection = _connectionFactory.GetConnection())
{
return await connection.QueryFirstAsync<Client>(query, parameters);
}
}
}
下面是一个搜索查询测试
public partial class ClientRepositoryTests
{
[Theory]
[InlineData("Data")]
public async Task GetClientBySearchCriteria_InValidInput_ReturnZeroRecords(string jsonDataKey)
{
var searchParameter = "5";
var builder = new TestBuilder(jsonDataKey).SetupClients()
.SetupProperties();
var repository = builder.Build();
var result = await repository.GetClientBySearchCriteria(searchParameter);
Assert.NotNull(result);
Assert.Equal(0, result.Count);
}
我试图重复一遍,但我有一个问题,因为我没有经验,也不了解它是如何工作的,我将感谢你的建议,并帮助我的决定
public partial class TownRepositoryTests
{
private class TestBuilder
{
public Mock<IConnectionFactory> MockConnectionFactory { get; set; }
private readonly IOptions<AppSettings> _appSettingsOptions;
public InMemoryDatabase _memoryDatabase;
public TestDataGenerator TestDataGenerator;
private const string _jsonDataPath = @"../../../TestData/Town/TownTestData.json";
private string _jsonDataKey;
public TestBuilder(string jsonDataKey)
{
MockConnectionFactory = new Mock<IConnectionFactory>();
_appSettingsOptions = MockSetup.GetAppSettingsOptions();
_memoryDatabase = new InMemoryDatabase();
MockConnectionFactory.Setup(c => c.GetConnection()).Returns(_memoryDatabase.OpenConnection());
TestDataGenerator = new TestDataGenerator(_jsonDataPath);
_jsonDataKey = jsonDataKey;
TestDataGenerator.LoadData(_jsonDataKey);
}
public ITownRepository Build()
{
return new TownRepository(MockConnectionFactory.Object
, _appSettingsOptions);
}
public TestBuilder InsertTown()
{
//What I need to do here?
}
}
}
首先,我认为要在inserttown方法中进行更改,我需要调用testdatagenerator的新方法(在执行此操作之前,我需要将其添加到testdatagenerator中),对吗?如果正确,从我需要的开始,因为我不理解它们的逻辑,我可以在testdatagenerator中添加insert查询吗?
好的,那之后我就开始做测试了。我可以在这里添加insert查询还是需要在其他地方添加它?如果没有,我需要在这一部分做什么?
public partial class TownRepositoryTests
{
[Theory]
[InlineData("Data")]
public async Task InsertTownName(string jsonDataKey)
{
??
}
}
暂无答案!
目前还没有任何答案,快来回答吧!