Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1045975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:04:11+00:00 2026-05-16T16:04:11+00:00

[TestMethod()] public void ShowSetup_SendsMessage_WhenShowSetupCommandCalled() { //Arrange Messenger.Reset(); MainViewModel target = new MainViewModel(); bool wasCalled

  • 0
    [TestMethod()]
    public void ShowSetup_SendsMessage_WhenShowSetupCommandCalled()
    {

        //Arrange
        Messenger.Reset();
        MainViewModel target = new MainViewModel();
        bool wasCalled = false;
        Messenger.Default.Register<NotificationMessage>(this,"Settings",(msg) => wasCalled = true);

        //Act
        target.ShowSetupCommand.Execute(null);

        //Assert
        Assert.IsTrue(wasCalled);
    }

I see there is an IMessenger interface and I tried to mock it and set Messenger.OverrideDefault to the mock like this:

var mock = new Mock<IMessenger>();      
Messenger.OverrideDefault((Messenger)mock.Object);

But I got an invalid cast error. Is the OverrideDefault method not for that purpose or more likely I’m using it incorrectly.

Or would I have an interface for the classes that are receiving the messages and mock those? All I really want to test is that a RelayCommand sends a message when it is called.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:04:11+00:00Added an answer on May 16, 2026 at 4:04 pm

    I just started to look at this myself. I’m a little surprised that Messenger.OverrideDefault doesn’t take an IMessenger as a parameter. You have to inherit Messenger.

    I suppose you could create a class that internally uses your mock object and then do a Verify.

            [Test]
        public void ShowSetup_SendsMessage_WhenShowSetupCommandCalled() {
            Messenger.Reset();
            MaintenanceViewModel target = new MainViewModel();
            IMessenger mockMessenger = MockRepository.GenerateMock<IMessenger>();
            mockMessenger.Expect(m => m.Send("Settings"));
            TestMessenger testMessenger = new TestMessenger(mockMessenger);
            Messenger.OverrideDefault(testMessenger);
            bool wasCalled = false;
            Messenger.Default.Register<NotificationMessage>(this, "Settings", (msg) => wasCalled = true);
            target.ShowSetupCommand.Execute(null);
    
            mockMessenger.VerifyAllExpectations();
        }
    

    You may or may not need a stub on the Register method.

    The TestMessenger class:

        public class TestMessenger : Messenger {
        private IMessenger _mockMessenger;
        public TestMessenger(IMessenger mock) {
            _mockMessenger = mock;
        }
        public override void Register<TMessage>(object recipient, bool receiveDerivedMessagesToo, Action<TMessage> action) {
            _mockMessenger.Register<TMessage>(recipient, receiveDerivedMessagesToo, action);
        }
    
        public override void Register<TMessage>(object recipient, Action<TMessage> action) {
            _mockMessenger.Register<TMessage>(recipient, action);
        }
    
        public override void Send<TMessage, TTarget>(TMessage message) {
            _mockMessenger.Send<TMessage, TTarget>(message);
        }
    
        public override void Send<TMessage>(TMessage message) {
            _mockMessenger.Send<TMessage>(message);
        }
    
        public override void Unregister<TMessage>(object recipient, Action<TMessage> action) {
            _mockMessenger.Unregister<TMessage>(recipient, action);
        }
    
        public override void Unregister<TMessage>(object recipient) {
            _mockMessenger.Unregister<TMessage>(recipient);
        }
    
        public override void Unregister(object recipient) {
            _mockMessenger.Unregister(recipient);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Shouldn't this test pass: [TestMethod] public void derived_message_subscription() { bool handled = false; Messenger.Default.Register<GenericMessage<baseClass>>(this,
Consider this unit test: [TestMethod] public void QueueManager_OnNextItem() { bool called = false; var
I have a test method: [TestMethod()] public void test_chars() { MyBO target = new
How can this test fail? [TestMethod] public void Get_Code() { var expected = new
Consider the following unit test: [TestMethod] public void TestByteToString() { var guid = new
Given the following Code. [TestMethod] public void CanResolveILoggerTest() { var Container = new Castle.Windsor.WindsorContainer();
Here is my code: [TestMethod] public void LoginUnregisteredUserShouldFail() { Mock<IRepository<User>> _repo = new Mock<IRepository<User>>();
//IsExist always false,is it a bug? [TestMethod] public void IsExist() { private Mock<IRepository> repository
[TestMethod] public void TestMethod1() { var mock = new Mock<EmailService>(); mock.Setup(x => x.SendEmail()).Returns(true); var
[TestMethod] public void Memory() { var wr = new WeakReference(aaabbb); Assert.IsTrue(wr.IsAlive); GC.Collect(); GC.Collect(); GC.Collect();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.