Last month I held a WPF presentation at work where I created a ViewModel class doing TDD. This got a little buzz because of the way I integrated BDD concepts in the tests. I have since then recieved a few question about how I actually did it. Let’s first have a look at what I did:
namespace Specifications_for_customer_search
{
[Scenario]
public class When_user_searches_for_customers : ScenarioBase
{
private RhinoAutoMocker<CustomerSearchViewModel> _mocks;
private List<Customer> _customers;
private string _customerName;
public override void Given()
{
_mocks = new RhinoAutoMocker<CustomerSearchViewModel>();
_customerName = "goran";
_customers = new List<Customer> { new Customer(_customerName) };
_mocks.Get<ICustomerRepository>().Stub(me => me.FindCustomerByName(Arg<string>.Is.Anything)).Return(_customers);
}
public override void When()
{
_mocks.ClassUnderTest.SearchCommand.Execute(_customerName);
}
[Then]
public void should_query_customer_repository_for_matching_customers()
{
_mocks.Get<ICustomerRepository>().AssertWasCalled(me => me.FindCustomerByName(_customerName));
}
[Then]
public void should_list_customers_found()
{
Assert.That(_mocks.ClassUnderTest.Customers, Is.EqualTo(_customers));
}
}
}
This was all done by using the base class from my
previous post and renaming
Arrange and
Assert to
Given and
When. In this example I used
NUnit, so I inherited from NUnit’s test attributes:
public class ThenAttribute : TestAttribute{}
public class ScenarioAttribute : TestFixtureAttribute { }
That was all. Happy testing! :)