考虑这个Viewmodel:
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。继承基类会使这很困难。 例如,这不起作用:
[Theory]
[AutoDomainData]
public void GoToConfiguration_ActivatesConfigurationView([Frozen]IConfigurationViewModel configVm,MainViewModel sut)
{
sut.GoToConfiguration();
Assert.True(configVm.IsActive);
}
答案 0 :(得分:2)
问题是因为MainViewModel
不是有效的,所以它不会激活ConfigurationViewModel
。
[Fact]
public void GoToConfigurationActivatesCorrectViewModel()
{
var configuration = new ConfigurationViewModel();
var main = new MainViewModel(configuration);
ScreenExtensions.TryActivate(main);
main.GoToConfiguration();
Assert.True(configuration.IsActive);
}
此测试首先将重要的一行传递给激活视图模型。