I am a "beginner" at TDD, and something I am trying to figure is how to unit test viewmodels...
I am wanting to make sure that a property ProeprtyChanged event is fired... I have the following test using nunit.
[Test]
public void Radius_Property_Changed()
{
var result = false;
var sut = new MainViewModel();
sut.PropertyChanged += (s, e) =>
{
if (e.PropertyName == "Radius")
{
result = true;
}
};
sut.Radius = decimal.MaxValue;
Assert.That(result, Is.EqualTo(true));
}
Is this the cleanest way to do this, or is there a better way to test this property
... snippet of code in the viewmodel of the propety I am testing looks like this...
public decimal Radius
{
get { return _radius; }
set
{
_radius = value;
OnPropertyChanged("Radius");
}
}
This is pretty much how you do that. There's not much else to do here given it's pretty simple (and boring) code. It might be worth to wrap that in your own reusable library/tool. Or even better, use existing code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With