Testing conventions

Jun 24, 2011 at 12:48 AM

Hello, I'm quite new to CM so forgive me if this is obvious. CM provides cool facilities with its conventions, but is there a way to automate a test that checks that a convention really does what I expect it to do?

Simple example, if I have a button with x:Name="DoSomething" how do I write a unit test that checks that upon button click the VM's DoSomething method is called? Then extend this questions to other conventions as well.

By the way, I noticed that the original Caliburn has test utilities which seem to be working in CM has well, but still I didn't find support for testing conventions.

Jun 24, 2011 at 10:10 AM

There is no simple way available at the moment, as far as I know. 
I would try forcing the conventions to run against the View just before it is passed to the validator, thus recreating the runtime configuration.
Unfortunately conventional binding occurs against a living VM instance, while Caliburn.Testability works usign VM type inspection. 
The best workaround I can think of - even though I didn't actually tried it - is creating a method similar to ViewModelBinder.Bind just for testing scenarios; this method should only work with VMs type information, skipping the parts involving the actual VM instance. It should be something along this:

 public static BindForTesting(Type viewModelType, DependencyObject view) {

	var element = View.GetFirstNonGeneratedView(view) as FrameworkElement;
    if(element == null) return;
    var namedElements = BindingScope.GetNamedElements(element);

            namedElements.Apply(x => x.SetValue(
            namedElements = BindActions(namedElements, viewModelType);
            namedElements = BindProperties(namedElements, viewModelType);
            HandleUnmatchedElements(namedElements, viewModelType);
#if WP7