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 6051411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:49:04+00:00 2026-05-23T07:49:04+00:00

I need to asset an action called by a mock component. public interface IDispatcher

  • 0

I need to asset an action called by a mock component.

 public interface IDispatcher
    {
     void Invoke(Action action);
    }

    public interface IDialogService
    {
      void Prompt(string message);
    }

    public class MyClass
    {
    private readonly IDispatcher dispatcher;
    private readonly IDialogservice dialogService;
    public MyClass(IDispatcher dispatcher, IDialogService dialogService)
    {
     this.dispatcher = dispatcher;
    this.dialogService = dialogService;
    }

    public void PromptOnUiThread(string message)
    {
     dispatcher.Invoke(()=>dialogService.Prompt(message));
    }
    }

    ..and in my test..

   [TestFixture]
    public class Test
    {
     private IDispatcher mockDispatcher;
     private IDialogService mockDialogService;
    [Setup]
    public void Setup()
    {
     mockDispatcher = MockRepository.GenerateMock<IDispatcher>();
     mockDialogService = MockRepository.GenerateMock<IDialogService>();
    }

    [Test]
    public void Mytest()
    {
     var sut = CreateSut();
    sut.Prompt("message");

    //Need to assert that mockdispatcher.Invoke was called
    //Need to assert that mockDialogService.Prompt("message") was called.
    }
     public MyClass CreateSut()
    {
      return new MyClass(mockDipatcher,mockDialogService);
    }
    }

Maybe I need to restructure the code, but confused on that. Could you please advise?

  • 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-23T07:49:04+00:00Added an answer on May 23, 2026 at 7:49 am

    You are actually testing this line of code:

    dispatcher.Invoke(() => dialogService.Prompt(message));
    

    Your class calls the mock to invoke a method on another mock. This is normally simple, you just need to make sure that Invoke is called with the correct arguments. Unfortunately, the argument is a lambda and not so easy to evaluate. But fortunately, it is a call to the mock which makes it easy again: just call it and verify that the other mock had been called:

    Action givenAction = null;
    mockDipatcher
      .AssertWasCalled(x => x.Invoke(Arg<Action>.Is.Anything))
      // get the argument passed. There are other solutions to achive the same
      .WhenCalled(call => givenAction = (Action)call.Arguments[0]);
    
    // evaluate if the given action is a call to the mocked DialogService   
    // by calling it and verify that the mock had been called:
    givenAction.Invoke();
    mockDialogService.AssertWasCalled(x => x.Prompt(message));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to know how to test this controller action def create_mobile if mobile_user
I have these asset ID's in a CMS and I need to update them
I also need to create a copy action in symfony. I already looked at
I need to count the number of views of an asset, this asset being
I need to get exif information from selected asset and to display part of
I need to assert the value of a variable in a workflow service. I've
I need a collection that behaves as Set and preserves order of element insertion.
I need some tips on how to work with assets in Symfony 2. For
I have created application where i just need to access data form assets folder.
Need to insert selected text on the page into textarea. There must be some

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.