Testing LiveUI applications

LiveUI based applications consist of application model and presentation. Application logic can be tested by unit tests whereas presenation can be tested only by functional tests. That's why it's better to make presentation layer thin and simple. Fortunately, if your application is simpe enoght you can use Web.Forms or Ext.Forms for presentation, in this case your application would consist of application model only and therefore would be easily testable.

Let's create simple form and test it by Unit tests. usually we need DataBase, so let's emulate one

public class Person

{

  public string Name { get; set;}

}

public static class DataBase

{

  private static readonly List<Person> persons = new List<Person>();

 

  public static List<Person> Persons {

    get { return persons; }

  }

}

Now let's create form for creating Persons.

public class PersonForm : Form

{

  public TextField PersonName { get; set;}

  public OkCancelButtonSet Buttons { get; set;}

 

  public override void OnInitializing()

  {

    // Add text field to input person name.

    PersonName = new TextField {Label = "Name"};

    Fields.Add(PersonName);

 

    // Add OK Cancel buttons.

    Buttons = new OkCancelButtonSet();

    Buttons.OkClick += buttons_OkClick;

    Footer = Buttons;

    base.OnInitializing();

  }

 

  void buttons_OkClick(object sender, EventArgs e)

  {

    DataBase.Persons.Add(new Person {Name = PersonName.Value});

  }

}

It's it, when user clicks OK button new Person should be added to the DataBase. Let's check it using unit test.

[TestFixture]

public class PersonFormTest

{

  [TestFixtureSetUp]

  public void SetUp()

  {

    var application = new Application();

    application.RegisterContracts(typeof(PersonFormTest).Assembly);

    application.Run();

  }

 

  public class MockFormView : Component, IFormView, IContract<Form, MockFormView> {

    public void ShowErrors(IEnumerable<string> errors) { }

  }

 

  public class MockTextFieldView : Component, ITextFieldView, IContract<TextField,MockTextFieldView>   {

    public string Value { get; set;}

  }

  public class MockButtonView : Component, IFormButtonView, IContract<FormButton, MockButtonView>

  {

    public event EventHandler<EventArgs> Click;

    public void FireClick() { Click(this, EventArgs.Empty); }

  }

 

  [Test]

  public void TestPesonFormCreatesPerson()

  {

    var root = new RootComponent();

    var personForm = new PersonForm();

    root.Children.Add(personForm);

    root.Initialize();

    personForm.PersonName.Value = "Alex";

    ((MockButtonView)personForm.Buttons.OkButton.View).FireClick();

    Assert.AreEqual("Alex", DataBase.Persons.Last().Name);

  }

}

There we mock classes to emulate presentation layer in unit test. If there were real application real Views would be instantiated.For example, let's create aspx page .

public partial class _Default : System.Web.UI.Page

{

  protected override void OnInit(EventArgs e)

  {

    base.OnInit(e);

    var rootComponent = new ExtFormsRootComponent {Page = this, Workspace = Page.Form};

    rootComponent.Children.Add(new PersonForm());

    rootComponent.Initialize();

  }

}

 

And real View appears