11 March 2009

BDD on Steroids

by mo


In the last couple of weeks I had a chance to sprinkle some of BDD syntactic sugar, all over my projects. It’s amazing how much more concise my unit tests have become. I’ve had a couple of issues where I was mocking out the behavior of some Win Forms controls, but for the most part it’s been an awesome experience!

public class behaves_like_save_changes_view_bound_to_presenter : concerns_for<SaveChangesView>
{
  context c = () => { presenter = an<ISaveChangesPresenter>(); };
  because b = () => subject.attach_to(presenter);

  static protected ISaveChangesPresenter presenter;
}

public class when_the_save_button_is_clicked : behaves_like_save_changes_view_bound_to_presenter
{
  because b = () => EventTrigger.trigger_event<Events.ControlEvents>(new EventArgs()), subject.ux_save_button);

  it should_forward_the_call_to_the_presenter = () => presenter.was_told_to(x => x.save());
}

public class when_the_cancel_button_is_clicked : behaves_like_save_changes_view_bound_to_presenter
{
  because b = () => EventTrigger.trigger_event<Events.ControlEvents>(x => x.OnClick(new EventArgs()),subject.ux_cancel_button);

  it should_forward_the_call_to_the_presenter = () => presenter.was_told_to(x => x.cancel());
}

public class when_the_do_not_save_button_is_clicked : behaves_like_save_changes_view_bound_to_presenter
{
  because b = () => EventTrigger.trigger_event<Events.ControlEvents>( x => x.OnClick(new EventArgs()), subject.ux_do_not_save_button);

  it should_forward_the_call_to_the_presenter = () => presenter.was_told_to(x => x.dont_save());
}
tdd tools csharp