简体   繁体   English

如何用构造函数进行单元测试

[英]How to make a Unit Test out of a Constructor

I am trying to make a Unit Test out of a public Constructor. 我正在尝试使用公共构造函数进行单元测试。

Using Visual Studio 2012 使用Visual Studio 2012

How can I create a simple Unit Test out of this Constructor. 如何使用此构造方法创建一个简单的单元测试。

    public ScenarioSelectViewModel(IEventRecorderSvc service)
    {
        eventRecordingSvc = svc;

        OkCmd = new DelegateCommand<Window>(
            OkCmd Executed,
            OkCmd CanExecute);

        RefreshAvailableScenarioStepExecutionsCommand = new       DelegateCommand<object>(
            RefreshAvailableScenarioStepExecutionsCommandExecuted,
            RefreshAvailableScenarioStepExecutionsCommandCanExecute);

        if (RefreshAvailableScenarioStepExecutionsCommand.CanExecute(null))
        {
            RefreshAvailableScenarioStepExecutionsCommand.Execute(null);
        }
    }

Assuming OkCmd and RefreshAvailableScenarioStepExecutionsCommand are publicly exposed properties inyour class ScenarioSelectViewModel. 假设OkCmdRefreshAvailableScenarioStepExecutionsCommand是类ScenarioSelectViewModel中公开公开的属性。

You are doing 2 things in the constructor which should be tested: 您正在构造函数中做两件事,应该对其进行测试:

  • Okmd and RefreshAvailableScenarioStepExecutionsCommand Initialization Okmd和RefreshAvailableScenarioStepExecutions命令初始化
  • Execution of RefreshAvailableScenarioStepExecutionsCommand Command. RefreshAvailableScenarioStepExecutionsCommand命令的执行。

You can start asserting the objects that are created, then asserting their publicly exposed properties, ie OkCmd.Execute and OkCmd.CanExecute to whatever you are expecting them to be. 您可以开始声明所创建的对象,然后声明其公开暴露的属性,即OkCmd.Execute和OkCmd.CanExecute到您期望的对象。 Same goes with RefreshAvailableScenarioStepExecutionsCommand . RefreshAvailableScenarioStepExecutionsCommand

Then you can assert the execution of the RefreshAvailableScenarioStepExecutionsCommand command 然后,您可以断言RefreshAvailableScenarioStepExecutionsCommand命令的执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM