Answer the question
In order to leave comments, you need to log in
How to force a method to fall with an exception?
There is a repository method that adds an object to the base (in my case the base is a list). However, if an empty object is passed to it, it must be thrown with an exception for further processing.
public bool Save(T entity)
{
entity = new T();
bool SaveResult = false;
if (entity != null)
{
AllItem.Add(entity);
SaveResult = true;
}
return SaveResult;
}
[TestMethod]
public void SaveNullObjectTest()
{
AccountModel ent = new AccountModel();
ent = null;
bool result = AccountBS.Save(ent);
Assert.IsFalse(result);
}
Answer the question
In order to leave comments, you need to log in
entity = new T();
Doesn't it bother you?
Object instance created, variable will not be null
public void Save(T entity)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
AllItem.Add(entity);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question