【问题标题】:How to verify order of mocked views in custom wizard object如何验证自定义向导对象中模拟视图的顺序
【发布时间】: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


    【解决方案1】:

    从示例中,您的设置

    mockContainer.Setup(c => c.GetInstance<FirstStepView>()).Returns(mockFirstStepView.Object);
    

    以及代码中调用的内容

    return IOCContainer.GetInstance(currentStep) as UserControl;
    

    不同。

    您的设置需要与打算在测试中使用的设置相匹配。

    mockContainer.Setup(c => c.GetInstance(typeof(FirstStepView)).Returns(mockFirstStepView.Object);
    

    【讨论】:

    • 虽然您完全正确,我的测试和我所说的需要匹配,但这并没有解决手头的问题。进行正确的设置调用并没有解决错误。
    • 您的视图可能依赖于仅在运行时资源字典中可用的内容。看起来它正在尝试检索它找不到的 XAML 文件。资源字典在启动时由框架填充,这可能没有在单元测试中完成
    【解决方案2】:

    我的问题是我试图在不需要的时候模拟视图。我已将我的测试代码更改为这样,现在我的测试运行良好。

    [TestMethod]
    public void TestFirstPageType()
    {
        Wizard testWizard = new Wizard();
    
        Mock<IContainer> mockContainer = new Mock<IContainer>();
        mockContainer.Setup(c => c.GetInstance<FirstStepView>()).Returns(new FirstStepView());
    
        testWizard.IOCContainer = mockContainer.Object;
        testWizard.Initialize();
    
        FirstStepView testView = testWizard.CurrentStep as FirstStepView;
        Assert.IsTrue(testView != null);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-20
      相关资源
      最近更新 更多