NavigationCommand CommandBinding does not seem to be working in my unit tests
I have a Wizard implementation in my WPF application. It's moreso setup as
a framework that people can plug a list of "pages". Part of this Wizard
implementation is to bind to the NavigationCommands.NextPage and
PreviousPage commands. This is done in code.
The bindings work just fine when running the application. They do not,
however, seem to work during unit testing.
Based on the code below, anytime NavigationCommands.NextPage(null, null)
is called, the nextCommand_executed() method should be hit.
Again...this works just fine during normal running of the application.
Under test, however, it does not.
Is there something obvious that I'm missing? My unit test does require
STAThread...but this is ok under the current testing circumstances (more
tests which require STAThread).
private readonly CommandBindingCollection _commandBindings = new
CommandBindingCollection();
public CommandBindingCollection CommandBindings
{ get { return _commandBindings; } }
public WizardMainViewModel(IWizardContent wizardContent)
{
var nextPageBinding = new CommandBinding(NavigationCommands.NextPage,
nextCommand_Executed, nextCommand_CanExecute);
var previousPageBinding = new
CommandBinding(NavigationCommands.PreviousPage, backCommand_Executed,
backCommand_CanExecute);
var browseBackBinding = new
CommandBinding(NavigationCommands.BrowseBack, okCommand_Executed,
okCommand_CanExecute);
//Register the bindings to this class
CommandManager.RegisterClassCommandBinding(typeof(WizardMainViewModel),
nextPageBinding);
CommandManager.RegisterClassCommandBinding(typeof(WizardMainViewModel),
previousPageBinding);
CommandManager.RegisterClassCommandBinding(typeof(WizardMainViewModel),
browseBackBinding);
//Adds the binding to the CommandBindingCollection
CommandBindings.Add(nextPageBinding);
CommandBindings.Add(previousPageBinding);
CommandBindings.Add(browseBackBinding);
}
private void nextCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
if (!WizardViewModel.CurrentPageVM.Validate())
{
}
//bunch of unnecessary to post code
}
THE TEST:
[Test]
public void MakeNextWork()
{
var vm = new FakeWizardViewModel();
var wizContent = new FakeWizardContent(vm);
var wiz = new Wizard(wizContent);
wiz.Show();
try
{
var dataContext = wiz.DataContext as WizardMainViewModel;
//wiz.Dispatcher.Invoke(new Action(() =>
NavigationCommands.NextPage.Execute(null, null)));
NavigationCommands.NextPage.Execute(null, null)));
var currentPageVM = vm.CurrentPageVM;
if (currentPageVM == null) Console.WriteLine("boo");
}
finally
{
wiz.Close();
}
}
public class FakeWizardContent : WizardContent
{
public FakeWizardContent(WizardViewModelBase viewModel)
: base(viewModel)
{
}
}
public class FakeWizardViewModel : WizardViewModelBase
{
public FakeWizardViewModel()
{
}
private List<WizardStepBaseViewModel> _vmPages;
public override List<WizardStepBaseViewModel> VMPages
{
get
{
if (_vmPages == null)
{
_vmPages = new List<WizardStepBaseViewModel>
{
Mock.Of<WizardStepBaseViewModel>(),
Mock.Of<WizardStepBaseViewModel>(),
Mock.Of<WizardStepBaseViewModel>()
};
CurrentPageVM = _vmPages[0];
}
return _vmPages;
}
}
public override string WizardTitleCaption
{
get { return string.Empty; }
}
}
No comments:
Post a Comment