如何在 Conductor 上对 ActivateItem 进行单元测试

How to unit test ActivateItem on a Conductor

考虑这个视图模型:

 public class MainViewModel : Conductor<object>
{
    private readonly IEventAggregator eventAggregator;
    private readonly IWindowManager windowManager;
    IConfigurationViewModel configurationViewModel;

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel(IEventAggregator eventAggregator,IWindowManager windowManager,IConfigurationViewModel configurationViewModel)
    {
        this.eventAggregator = eventAggregator;
        this.windowManager = windowManager;
        this.eventAggregator.Subscribe(this);
        this.configurationViewModel = configurationViewModel;
    }

    public void GoToConfiguration()
    {
        ActivateItem(configurationViewModel);
    }

}

 public interface IConfigurationViewModel:IScreen
{

}
public class ConfigurationViewModel :Screen, IConfigurationViewModel
{

}

这可能只是一个简单的示例,但我想测试 GoToConfigurationMethod。从基础 class 继承使得这很困难。 这例如不起作用:

 [Theory]
    [AutoDomainData]
    public void GoToConfiguration_ActivatesConfigurationView([Frozen]IConfigurationViewModel configVm,MainViewModel sut)
    {
        sut.GoToConfiguration();
        Assert.True(configVm.IsActive);
    }

问题是因为 MainViewModel 未激活,所以它不会激活 ConfigurationViewModel

[Fact]
public void GoToConfigurationActivatesCorrectViewModel()
{
    var configuration = new ConfigurationViewModel();
    var main = new MainViewModel(configuration);

    ScreenExtensions.TryActivate(main);

    main.GoToConfiguration();

    Assert.True(configuration.IsActive);
}

此测试通过了重要的一行,即首先激活视图模型。