2

I have a simple ViewModel which contains some buttons. The visibility of these buttons can be changed by events raised with the EventAggregator of PaP Prism which is also the only constructor parameter of this VM. The corresponding test works just fine when I don't use 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);
}

Unfortunately, it does not function like it is given here because the EventAggregator instance injected to the ViewModel by AutoFixture is another instance than the one injected into the test.

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;
    }
}

It seems to me, that the [Frozen] attribute does not work as expected but I am also not sure if a did every thing right. I was also wondering why Inject, instead of Register, does not work. I would have expected that their is no difference.

Thank you for your help.

Edit: I use version 3.7

4

1 回答 1

5

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

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

应该做的伎俩。

于 2013-09-21T06:15:43.203 回答