Previous Entry Share Next Entry
I did it!

After... what, 5 different tries? I finally found a model that is _WORKING_ for me. I have yet to put in the (9+12)*2 unit tests to prove it to myself (or, less, if I use MbUnit's Row tests), but, very quickly, an example of how it works, i'll try to make a "real" post (somewhere? codeproject?) where I can detail it further:
public class DefaultController : GenericController
    public DefaultController(Control targetUi, bool build):base(targetUi,build)
        VerifyControl("Greeting", typeof(Label));
        VerifyControl("Name", typeof(TextBox));
        VerifyProperty("MyInt", 0); 

    public void Button1Click()
        // problem 1: how do we get "5" from the UI? 
        int i = (int)GetProperty("MyInt");
        SetProperty("MyInt", i);
        GetLabel("Greeting").Text = "Hello World " + GetTextBox("Name").Text + " "+ i.ToString();

This is what the test code looks like:
        DefaultController c2 = new DefaultController(new Control(), true);
        c2.GetTextBox("Name").Text = "Barney";
        c2.SetProperty("MyInt", 6);
        Assert.AreEqual("Hello World Barney 7",c2.GetLabel("Greeting").Text); 

This is what the actual UI implementation looks like:
    DefaultController controller; 
    protected void Page_Load(object sender, EventArgs e)
        controller = new DefaultController(this, false); 
    protected void Button1_Click(object sender, EventArgs e)
    private int _myInt = 5;
    public int MyInt { get { return _myInt; } set { _myInt = value; } } 

The things I appreciate about it are:
  • For mocking controls, it uses real control objects. That means all the .Visible, .Enabled, all that stuff -- can be mocked.
  • The Controller is _required_ to call VerifyControl() -- during runtime, it does check the types, and during mocktime, it creates the objects needed. It also copies the controls into a seperate dictionary so that only the controls to be mocked are searched for.
  • I was able to put in (using reflection) a "mirror" of any property defined on the target UI. This is useful for things like DNN's UserId and ModuleId properties, etc.
  • The bulk of the code is in the GenericController class. This is as it should be -- the controller itself, and the UI that use it, should not be bulked up.
  • While developing functional tests (which have a log that gets displayed on the screen), i can call CurrentState on the controller -- and it gives me a textual representation of the UI.
    C: Greeting: (Label) Hello World Barney 7
    C: Name: (TextBox) Barney
    P: MyInt: 7 (Int32)
    And although I haven't tested it, it should do that for a live object too. Hmm.

Anyhows. True unit testing folks would frown upon what I'm doing here, I think -- its not very pretty. But hey, it works for me, and for my client, so this is completely awesome and cool.

*very tired*... 1am.. i'm going to drive out by the trash, drop off the smelly litter, go to Meijers, get: Milk, matches, drink a hot cup of milk, and Go to Zzz land.

  • 1
Don't you just love Meijer?

I certainly do.
I'm starting to be friends with some of the people who work there, too! ... their eyes light up when they see me, because I always specifically ask them how they're doing, and wish them a wonderful day.

  • 1

Log in

No account? Create an account