gpt4 book ai didi

c# - 我应该使用数据库模拟进行单元测试吗

转载 作者:太空宇宙 更新时间:2023-11-03 10:43:03 28 4
gpt4 key购买 nike

我目前正在为应用程序编写各种单元测试。

现在,我确实有测试来检查代码是否正常工作,确实如此。但是我是否也应该模拟,例如:

  • 数据库不可用。
  • 数据库查询返回空值。
  • 数据库查询需要很长时间才能执行。

我知道这不是一个编码问题,但我想对此有一些一般性的想法。

如果需要,我想到了下面的方法:

SettingsUnavailableMock.Setup(x => x.PageRepository.All()).Throws(new Exception());
SettingsUnavailableMock.Setup(x => x.PageRepository.Get(It.IsAny<int>())).Throws(new Exception());
SettingsUnavailableMock.Setup(x => x.PageRepository.Get(It.IsAny<string>())).Throws(new Exception());
SettingsUnavailableMock.Setup(x => x.PageRepository.Refresh(It.IsAny<Page>())).Throws(new Exception());
SettingsUnavailableMock.Setup(x => x.PageRepository.Save()).Throws(new Exception());

当然,为所有存储库添加。

然后在我的测试类中,我可以选择我想使用的 Mock。

最佳答案

理想情况下,您应该测试以上所有内容;但是,这取决于您的情况。就个人而言,我总是测试我可以测试的所有内容。

需要很长时间的查询是很现实的。数据库不可用也是很现实的。

查询返回 null 我不太确定;然而,如果这是一个现实的场景,那么一定要 stub 并测试它。

更新 - 基于评论,我认为添加这将是一件好事

public interface IRepository<T> where T : IRepositoryEntry, new()
{
event EventHandler<RepositoryOperationEventArgs> InsertEvent;
event EventHandler<RepositoryOperationEventArgs> UpdateEvent;
event EventHandler<RepositoryOperationEventArgs> DeleteEvent;
IList<String> PrimaryKeys { get; }

void Insert(T Entry);
void Update(T Entry);
void Delete(Predicate<T> predicate);
bool Exists(Predicate<T> predicate);
T Retrieve(Predicate<T> predicate);

IEnumerable<T> RetrieveAll();

}

public interface IRepositoryEntry
{
IList<String> GetPrimaryKeys();
}

public class OracleRepository
{

const string ConnectionString = "*"

public static IDbConnection GetIDbConnection()
{
IDbConnection connection = new OracleConnection(ConnectionString).OpenConnection();
return connection;
}
public IDbConnection GetConnection()
{
IDbConnection connection = new OracleConnection(ConnectionString).OpenConnection();
return connection;
}

}

public class OracleRepository<T> : OracleRepository, IDisposable, IRepository<T> where T : RepositoryEntryBase, IRepositoryEntry, new()
{


/// <summary>
/// Gets all property names from a type.
/// </summary>
/// <returns>IEnumerable of strings</returns>
static IEnumerable<String> GetEntryPropertyNames(Type type)
{
foreach (var propInfo in type.GetProperties())
yield return propInfo.Name;
}

public event EventHandler<RepositoryOperationEventArgs> InsertEvent;
public event EventHandler<RepositoryOperationEventArgs> UpdateEvent;
public event EventHandler<RepositoryOperationEventArgs> DeleteEvent;

#region Properties
public IList<String> PrimaryKeys
{
get
{
return primaryKeys.AsReadOnly();
}
private set
{
primaryKeys = new List<String>(value);
}
}
public IList<String> Properties { get; private set; }

public String InsertText { get; private set; }
public String UpdateText { get; private set; }
public String DeleteText { get; private set; }
public String SelectText { get; private set; }
#endregion

#region Fields
IDbConnection connection;
IDbTransaction transaction;
List<String> primaryKeys;
#endregion

#region Constructors
public OracleRepository()
{
PrimaryKeys = new List<String>(new T().GetPrimaryKeys());
Properties = new List<String>(GetEntryPropertyNames(typeof(T))).AsReadOnly();
InsertText = GenerateInsertText();
UpdateText = GenerateUpdateText();
SelectText = GenerateSelectText();
DeleteText = GenerateDeleteText();
connection = GetConnection();
}
#endregion

#region Interface Implementations
public void Insert(T Entry)
{
Insert(connection, Entry);
}
public void Update(T Entry)
{
Update(connection, Entry);
}
public void Delete(Predicate<T> predicate)
{
Delete(connection, predicate);
}

public T Retrieve(Predicate<T> predicate)
{
return Retrieve(connection, predicate);
}
public bool Exists( Predicate<T> predicate)
{

return Exists(connection, predicate);
}

public IEnumerable<T> RetrieveAll()
{
return RetrieveAll(connection);
}

public void Dispose()
{
if (transaction != null)
transaction.Dispose();
connection.Dispose();
}
#endregion

#region Public Methods

public void StartTransaction()
{
if (transaction != null)
throw new InvalidOperationException("Transaction is already set. Please Rollback or commit transaction");
transaction = connection.BeginTransaction();
}
public void CommitTransaction()
{
transaction.Commit();
transaction.Dispose();
transaction = null;
}
public void RollbackTransaction()
{

transaction.Rollback();
transaction.Dispose();
transaction = null;
}


public void Insert(IDbConnection connection, T Entry)
{
Type type = typeof(T);
List<Object> args = new List<Object>();
for (int i = 0; i < Properties.Count; i++)
args.Add(type.GetProperty(Properties[i]).GetValue(Entry));
connection.NonQuery(InsertText, args.ToArray());
if (InsertEvent != null)
InsertEvent(this, new OracleRepositoryOperationEventArgs() { Entry = Entry, Transaction = (transaction != null) });
}

public void Update(IDbConnection connection, T Entry)
{
Type type = typeof(T);
List<Object> args = new List<Object>();

foreach (var propertyName in Properties.Where(p => !PrimaryKeys.Any(k => k == p)))
args.Add(type.GetProperty(propertyName).GetValue(Entry));
foreach (var PropertyName in PrimaryKeys)
args.Add(type.GetProperty(PropertyName).GetValue(Entry));

connection.NonQuery(UpdateText, args.ToArray());
if (UpdateEvent != null)
UpdateEvent(this, new OracleRepositoryOperationEventArgs() { Entry = Entry, Transaction = (transaction != null) });

}


public void Delete(IDbConnection connection, Predicate<T> predicate)
{
var entryList = RetrieveAll(connection).Where(new Func<T, bool>(predicate));
Type type = typeof(T);

foreach(var entry in entryList)
{
List<Object> args = new List<Object>();
foreach (var PropertyName in PrimaryKeys)
args.Add(type.GetProperty(PropertyName).GetValue(entry));
connection.NonQuery(DeleteText, args.ToArray());
if (DeleteEvent != null)
DeleteEvent(this, new OracleRepositoryOperationEventArgs() { Entry = null, Transaction = (transaction != null) });

}
}

public T Retrieve(IDbConnection connection, Predicate<T> predicate)
{
return RetrieveAll(connection).FirstOrDefault(new Func<T, bool>(predicate));
}
public bool Exists(IDbConnection connection, Predicate<T> predicate)
{
return RetrieveAll(connection).Any(new Func<T, bool>(predicate));
}

public IEnumerable<T> RetrieveAll(IDbConnection connection)
{
List<T> collection = new List<T>();
var result = connection.Query(SelectText);
foreach (var row in result.Tuples)
collection.Add(RepositoryEntryBase.FromPlexQueryResultTuple(new T(), row) as T);
return collection;
}
#endregion

#region Private Methods

String GenerateInsertText()
{
String statement = "INSERT INTO {0}({1}) VALUES ({2})";
//Do first entry here becasse its unique input.
String columnNames = Properties.First();

String delimiter = ", ";
String bph = ":a";

String placeHolders = bph + 0;

//Start @ 1 since first entry is already done
for (int i = 1; i < Properties.Count; i++)
{
columnNames += delimiter + Properties[i];
placeHolders += delimiter + bph + i;
}

statement = String.Format(statement, typeof(T).Name, columnNames, placeHolders);
return statement;
}

String GenerateUpdateText()
{
String bph = ":a";
String cvpTemplate = "{0} = {1}";
String statement = "UPDATE {0} SET {1} WHERE {2}";

//Can only set Cols that are not a primary Keys, Get those Columns
var Settables = Properties.Where(p => !PrimaryKeys.Any(k => k == p)).ToList();

String cvp = String.Format(cvpTemplate, Settables.First() , bph + 0 );
String condition = String.Format(cvpTemplate, PrimaryKeys.First(), bph + Settables.Count);

//These are the values to be set | Start @ 1 since first entry is done above.
for (int i = 1; i < Settables.Count; i++)
cvp += ", " + String.Format(cvpTemplate, Settables[i], bph + i);

//This creates the conditions under which the values are set. | Start @ 1 since first entry is done above.
for (int i = Settables.Count + 1; i < Properties.Count; i++)
condition += ", " + String.Format(cvpTemplate, PrimaryKeys[i - Settables.Count], bph + i);

statement = String.Format(statement, typeof(T).Name, cvp, condition);
return statement;
}

String GenerateDeleteText()
{
String bph = ":a";
String cvpTemplate = "{0} = {1}";
String statement = "DELETE FROM {0} WHERE {1}";
String condition = String.Format(cvpTemplate, PrimaryKeys.First(), bph + 0);

for (int i =1; i < PrimaryKeys.Count; i++)
condition += ", " + String.Format(cvpTemplate, PrimaryKeys[i], bph + i);

statement = String.Format(statement, typeof(T).Name, condition);
return statement;
}

String GenerateSelectText()
{
String statement = "SELECT * FROM {0}";
statement = String.Format(statement, typeof(T).Name);
return statement;
}
#endregion
}

这是实现 IReposistoryEntry 的元素的样子:

public class APPS : RepositoryEntryBase, IRepositoryEntry
{
public int APP_ID { get; set; }
public string AUTH_KEY { get; set; }
public string TITLE { get; set; }
public string DESCRIPTION { get; set; }
public int IS_CLIENT_CUSTOM_APP { get; set; }


public APPS() : base() {
primaryKeys.Add("APP_ID");
}
public APPS(PlexQueryResultTuple plexTuple) : base(plexTuple) { }
}

public class RepositoryEntryBase
{

public static RepositoryEntryBase FromPlexQueryResultTuple( RepositoryEntryBase reb, PlexQueryResultTuple plexTuple)
{

if (plexTuple.parent == null)
throw new NotSupportedException("This Operation is Not supported by this PlexTuple.");

Type type = reb.GetType();
var pInfo = type.GetProperties();
PlexQueryResult result = plexTuple.parent;

foreach (var p in pInfo)
{
int index = result.Tuples.IndexOf(plexTuple);

if (result[p.Name, index] == null)
continue;

var conversationType = Nullable.GetUnderlyingType(p.PropertyType) ?? p.PropertyType;
object value = Convert.ChangeType(result[p.Name, index], (result[p.Name, index] != null)?conversationType: p.PropertyType);
p.SetValue(reb, value);
}
return reb;
}

protected IList<String> primaryKeys;

public RepositoryEntryBase()
{
primaryKeys = new List<String>();
}
public RepositoryEntryBase(PlexQueryResultTuple plexTuple) : this()
{
FromPlexQueryResultTuple(this, plexTuple);
}


public IList<String> GetPrimaryKeys()
{
return primaryKeys;
}
}

下面我发布了模拟数据库。在这里要认识到的重要一点是,测试实际上使用了接口(interface),我可以很容易地将真实数据库与模拟数据库进行交换。我喜欢大量重复使用这段代码(对我来说它实际上是在一个 dll 中)。所以我不必为每个项目重新编写数据库代码。

public class InMemoryRepository<T> : IRepository<T> where T :  IRepositoryEntry, new()
{
//RepositoryEntryBase,
public event EventHandler<RepositoryOperationEventArgs> InsertEvent;
public event EventHandler<RepositoryOperationEventArgs> UpdateEvent;
public event EventHandler<RepositoryOperationEventArgs> DeleteEvent;

public IList<String> PrimaryKeys { get; protected set; }
List<T> data;
public InMemoryRepository() {
PrimaryKeys = new List<String>(new T().GetPrimaryKeys());
data = new List<T>();
}

public void Insert(T Entry){
if(Get(Entry) != null)
throw new Exception("Duplicate Entry - Identical Key already exists");
data.Add(Entry);
if (InsertEvent != null)
InsertEvent(this, new RepositoryOperationEventArgs() { Entry = Entry });
}

public void Update(T Entry){
var obj = Get(Entry);
if (obj == null)
throw new Exception("Object does not exist");
obj = Entry;
if (UpdateEvent != null)
UpdateEvent(this, new RepositoryOperationEventArgs() { Entry = obj });
}

public void Delete(Predicate<T> predicate)
{
data.RemoveAll(predicate);
if (DeleteEvent != null)
DeleteEvent(this, new RepositoryOperationEventArgs() { Entry = null });
}

public bool Exists(Predicate<T> predicate)
{
return data.Exists(predicate);
}

public T Retrieve(Predicate<T> predicate)
{
return data.FirstOrDefault(new Func<T, bool>(predicate));
}

public IEnumerable<T> RetrieveAll()
{
return data.ToArray();
}

T Get(T Entry)
{
//Returns Entry based on Identical PrimaryKeys
Type entryType = typeof(T);
var KeyPropertyInfo = entryType.GetProperties().Where(p => PrimaryKeys.Any(p2 => p2 == p.Name));
foreach (var v in data)
{
//Assume the objects are identical by default to prevent false positives.
Boolean AlreadyExists = true;
foreach (var property in KeyPropertyInfo)
if (!property.GetValue(v).Equals(property.GetValue(Entry)))
AlreadyExists = false;
if (AlreadyExists)
return v;
}
return default(T);
}
}

关于c# - 我应该使用数据库模拟进行单元测试吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24574232/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com