lundi 19 septembre 2016

How can I test that an MVVM light message has been received and acted upon?

I have a derived class that gets an object via property injection and registers on the messenger of that object:

public class Foo : AbsFoo
{
    private IBar bar;
    public override IBar Bar 
    {
        get {return bar;}
        set
        {
            bar = value
            if(bar != null)
            {
                bar.Messenger.Register<MyMessage>(this, m => SomeMethod());
            }
        }
    }
    public override void SomeMethod()
    {
        //..
    }
}

Basically, I want to set Bar, send a message, and verify that SomeMethod() is called.

My test looks like this:

var fixture = new Fixture();
fixture.Customize(new AutoConfiguredMoqCustomization());
var messenger = new Messenger();

var barFixture = fixture.Create<IBar>();
barFixture.Messenger = messenger

var fooMock = new Mock<Foo> {CallBase = true};
fooMock.SetupAllProperties();
fooMock.Object.Bar = barFixture;
fooMock.VerifySet(s=> s.Bar = It.IsAny<IBar>(),Times.AtLeastOnce()); // Success

messenger.Send(new MyMessage());
fooMock.Verify(c => c.SomeMethod(), Times.Once);  // Fails   

VerifySet() succeeds, and the correct object is passed in (checked via debugging), and the messenger instances are the same. But the Verifyon the method call fails, and I don't really understand why.

I'm not quite sure about the setup methods I have to use (Setup? SetupSet? Another?) on fooMock

Aucun commentaire:

Enregistrer un commentaire