【发布时间】:2016-08-16 15:42:36
【问题描述】:
我有一个 WPF MVVM 应用程序,我需要在其中创建一个自定义向导类型,其中包含控制各种视图流的对象。该向导包含一个私有的内部类型列表,这些类型稍后会创建为实际项目,并且具有操作您所在视图的方法。
向导工作得非常好;现在是时候对其进行单元测试以确保视图的顺序正确。
向导有一个 Initialize 调用来指定它的视图顺序。 NextStep 和 PreviousStep 用于更改您当前所在的视图(它们实际上只是更新索引并引发属性更改事件),并且外部对象通过提供视图的 CurrentStep 属性访问当前视图。它使用 StructureMap IContainer 来实例化这些视图。
public class WizardExample
{
private IList<Type> _steps = new List<Type>();
private int _currentStepIndex = 0;
public void Initialize()
{
_steps.Add(typeof(FirstStepView));
_steps.Add(typeof(SecondStepView));
_steps.Add(typeof(ThirdStepView));
}
public UserControl CurrentStep
{
get
{
Type currentStep = _steps[_currentStepIndex];
return IOCContainer.GetInstance(currentStep) as UserControl;
}
}
public IContainer IOCContainer { get; set; }
public void NextStep()
{
if(_currentStepIndex < _steps.Count - 1)
{
++_currentStepIndex;
}
}
public void PreviousStep()
{
if(_currentStepIndex > 0)
{
--_currentStepIndex;
}
}
}
我想要执行的测试是这样的(我使用的是 Moq 和 MSTest):
[TestMethod]
public void TestFirstPageType()
{
Wizard testWizard = new Wizard();
Mock<FirstStepView> mockFirstStepView = new Mock<FirstStepView>();
mockFirstStepView.SetupAllProperties();
Mock<IContainer> mockContainer = new Mock<IContainer>();
mockContainer.Setup(c => c.GetInstance<FirstStepView>()).Returns(mockFirstStepView.Object);
testWizard.IOCContainer = mockContainer.Object;
testWizard.Initialize();
FirstStepView testView = testWizard.CurrentStep as FirstStepView;
Assert.IsTrue(testView != null);
}
但是,当我去执行这个测试时,我得到了以下错误:
'System.Reflection.TargetInvocationException: 异常已被 由调用的目标抛出。 ---> System.Expcetion: 组件“Castle.Proxies.FirstStepViewProxy”没有资源 由 URI 标识 '/MyDll;component/wizard/views/firststepview.xaml'
知道我做错了什么吗?
【问题讨论】:
标签: c# wpf unit-testing mvvm moq