WPF MVVM and Unit Testing

I am working on a WPF application and I am using the ModelViewViewModel model template. I have a series of events that go out of view, which leads to ViewModel activity.

What is the resonant way to get these events from UnitTest? For example, I want to simulate a drop event. I really don't want to create a stub view, just to raise an event.

Any suggestions are welcome.

Thanks.

+5
source share
4 answers

According to the MVVM pattern:

  • View knows about ViewModel - it will refer to it either as a specific instance or interface
  • ViewModel should not know about viewing at all.

, , :

1: viewmodel . , :

class MyViewModel
{
    public ICommand ClickCommand { get; set; }
}

<Button Command="{Binding Path=ClickCommand}" />

, , myViewModel.ClickCommand.Execute .

2: viewmodel .xaml.cs , :

class MyViewModel
{
    public void HandleClick(){ }
}

<Button Click="MyClickHandler">

//.xaml.cs file
public void MyClickHandler( Object sender, EventArgs e ) {
    m_viewModel.HandleClick()
}

, , myViewModel.HandleClick . , MyClickHandler, 1 !

+16

, drop ViewModel. , , , ViewModel? , unit test ( , ViewModel).

, ViewModel .

+1

, ( , , , ). , Caliburn (http://www.codeplex.com/caliburn), "".

0

All Articles