AutoFixture.Xunit的[Frozen]无效

时间:2013-09-20 20:48:26

标签: c# xunit.net autofixture automoq

我有一个简单的ViewModel,它包含一些按钮。使用PaP Prism的EventAggregator引发的事件可以更改这些按钮的可见性,这也是此VM的唯一构造函数参数。当我不使用AutoFixture时,相应的测试工作正常。

[Theory]
[InfrastructureAutoData]
public void AllButtonsAreShownWhenVisibilityStatusIsSet(
    [Frozen]EventAggregator eventAggregator,
    ActionBarViewModel sut)
{
    eventAggregator
        .GetEvent<ActionButtonActivationEvent>()
        .Publish(VisibleActionButtons.All);

    sut.CancelButtonVisibility.Should().Be(Visibility.Visible);
    sut.BackButtonVisibility.Should().Be(Visibility.Visible);
    sut.NextButtonVisibility.Should().Be(Visibility.Visible);

    sut.Visiblity.Should().Be(Visibility.Visible);
}

不幸的是,它的功能不像这里给出的那样,因为AutoFixture注入ViewModel的EventAggregator实例是另一个实例,而不是注入测试的实例。

public class InfrastructureAutoData : AutoDataAttribute
{
    public InfrastructureAutoData()
    {
        Initialize();
    }

    private void Initialize()
    {
        this.Fixture.Customize(new AutoMoqCustomization());
        Fixture.Register<IEventAggregator>(() => new EventAggregator());
    }
}

public class ActionBarViewModel
{
    public ActionBarViewModel(IEventAggregator eventAggregator)
    {
        eventAggregator.GetEvent<ActionButtonActivationEvent>()
                       .Subscribe(ActivateButtons);

        ActivateButtons(VisibleActionButtons.None);
    }

    /// <summary>
    /// Visibility of a button which cancels the current action.
    /// </summary>
    public Visibility CancelButtonVisibility { get; private set; }

    /// <summary>
    /// Visibility of a button which loads the previous screen.
    /// </summary>
    public Visibility BackButtonVisibility { get; private set; }

    /// <summary>
    /// Visibility of a button with which the next step can be reached.
    /// </summary>
    public Visibility NextButtonVisibility { get; private set; }

    /// <summary>
    /// Visibility of the complete view which will be automatically
    /// set by the visibile buttons.
    /// </summary>
    public Visibility Visiblity { get; private set; }

    private void ActivateButtons(VisibleActionButtons buttonVisibility)
    {
        if (buttonVisibility == VisibleActionButtons.All)
        {
            NextButtonVisibility =
                CancelButtonVisibility =
                    BackButtonVisibility = Visibility.Visible;
        }
        else
        {
            NextButtonVisibility =
                buttonVisibility == VisibleActionButtons.Next
                                    ? Visibility.Visible
                                    : Visibility.Hidden;

            CancelButtonVisibility =
                buttonVisibility == VisibleActionButtons.Cancel
                                    ? Visibility.Visible
                                    : Visibility.Hidden;

            BackButtonVisibility =
                buttonVisibility == VisibleActionButtons.Back
                                    ? Visibility.Visible
                                    : Visibility.Hidden;
        }

        Visiblity =
            buttonVisibility == VisibleActionButtons.None
                                ? Visibility.Collapsed
                                : Visibility.Visible;
    }
}

在我看来,[Frozen]属性不能按预期工作,但我也不确定是否一切都做对了。我也想知道为什么Inject而不是Register不起作用。我原以为他们没有区别。

感谢您的帮助。

编辑:我使用版本3.7

1 个答案:

答案 0 :(得分:5)

你冻结了具体的类EventAggregator,但注入了接口IEventAggregator。它们是两种不同的类型,因此您获得的IEventAggregator实例不是冻结的EventAggregator

[Frozen(As = typeof(IEventAggregator))]EventAggregator eventAggregator

应该这样做。