Unit Testing the View Model

Topics: Getting Started
Dec 16, 2011 at 9:59 AM

Hello,

I am attempting to unit test my view model in isolation from the view. However, most of my view model logic begins when Caliburn calls into the protected OnViewLoaded() method. This method cannot be called from within a unit test as it is protected.

Is there a workaround that will help?

Thanks,

Tom

Jan 16, 2013 at 6:44 PM

I am also looking to do the same thing.  How does one start the Caliburn Micro View Model process in a unit test without loading a view?

Jul 18, 2013 at 8:15 AM
Bump.

I've just run in to this problem as well. I have some logic in the OnViewLoaded() override (not much, but there's still something I'd like to test), and I've tried desperately to find a solution to this. I'm struggling to even find a way to do it after binding the view to the viewmodel. Google isn't being much help unfortunately, and I thought google knew everything. :-s

Any help appreciated.
Jul 22, 2013 at 5:40 PM
You could always have your own method call in the OnViewLoaded() that does all of the logic.
protected override OnViewLoaded()
{
     MyLogic();
}

public void MyLogic()
{
     //Do stuff
}
You can then unit test MyLogic.
You should also be able to use ViewModelBinder.Bind(ViewModel, view, context). It calls AttachView() which has the handler that calls OnViewLoaded().