I'm writing tests for the following class:
public class Foo : INotifyPropertyChanged
{
private int _failCount;
private int _totalCount;
public double FailRate
{
get
{
double returnValue = 0.0;
if (_totalCount > 0)
{
returnValue = (double) _failCount / _totalCount * 100;
}
return returnValue;
}
}
public int FailCount
{
get { return _failCount; }
set
{
if (value != _failCount)
{
_failCount = value;
onNotifyPropertyChanged();
onNotifyPropertyChanged("FailRate");
}
}
}
public int TotalCount
{
get { return _totalCount; }
set
{
if (value != _totalCount)
{
_totalCount = value;
onNotifyPropertyChanged();
onNotifyPropertyChanged("FailRate");
}
}
}
protected virtual void onNotifyPropertyChanged([CallerMemberName]string name = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
I was writing some tests for the FailRate
property, and it occurred to me that I should probably make sure that getting FailRate
doesn't throw an exception. The case being someone (probably me) removes the if (_totalCount > 0)
check, and then getting FailRate
throws a DivideBy0Exception
.
So I wrote a test that makes sure getting FailRate
doesn't throw an exception.
[TestMethod]
public void GettingTheFailRateWhenTheTotalCountIsZeroDoesNotThrowAnException()
{
Defect defect = new Defect();
defect.FailCount = 0;
defect.TotalCount = 0;
double dummy = defect.FailRate;
}
However, that test doesn't have an explicit check, and will pass as long as the property doesn't throw an exception.
Is just checking that a property doesn't throw an exception a "valid" test?