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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:50:41+00:00 2026-05-20T09:50:41+00:00

[unit testing newbie] [c#] Consider the following scenario: I’m using Silverlight and calling a

  • 0

[unit testing newbie] [c#]

Consider the following scenario:

I’m using Silverlight and calling a WCF service. Silverlight can only call WCF services asynchronously. I build a wrapper around the WCF service so that I can work with Action parameters. (makes the client code a lot cleaner).

So I have an async service that retrieves meeting rooms.

public interface IMeetingRoomService
{
    void GetRooms(Action<List<MeetingRoom>> result);
}

Turning GetRooms into List<MeetingRoom> GetRooms() is not an option.

I want to use this service in a ViewModel to set a public property called Rooms.

public class SomeViewModel
{
    private readonly IMeetingRoomService _meetingRoomService;

    public List<MeetingRoom> Rooms { get; set; }

    public SomeViewModel(IMeetingRoomService meetingRoomService)
    {
        this._meetingRoomService = meetingRoomService;
    }

    public void GetRooms()
    {
        // Code that calls the service and sets this.Rooms
        _meetingRoomService.GetRooms(result => Rooms = result);
    }
}

I want to unit test the implementation of SomeViewModel.GetRooms().
(For this question I quickly wrote the implementation but I’m actually trying to use TDD.)

How do I finish this test?
I’m using NUnit and Moq.

[Test]
public void GetRooms_ShouldSetRooms()
{
    var theRooms = new List<MeetingRoom>
                       {
                           new MeetingRoom(1, "some room"),
                           new MeetingRoom(2, "some other room"),
                       };

    var meetingRoomService = new Mock<IMeetingRoomService>();

    //How do I setup meetingRoomService so that it gives theRooms in the Action??


    var viewModel = new SomeViewModel(meetingRoomService.Object);

    viewModel.GetRooms();

    Assert.AreEqual(theRooms, viewModel .Rooms);
}

EDIT:

Solution

Read Stephane’s answer first.

This is the Test code I ended up writing thanks to stephane’s answer:

[Test]
public void GetRooms_ShouldSetRooms()
{
    var meetingRoomService = new Mock<IMeetingRoomService>();
    var shell = new ShellViewModel(meetingRoomService.Object);
    var theRooms = new List<MeetingRoom>
                       {
                           new MeetingRoom(1, "some room"),
                           new MeetingRoom(2, "some other room"),
                       };

    meetingRoomService
        .Setup(service => service.GetRooms(It.IsAny<Action<List<MeetingRoom>>>()))
        .Callback((Action<List<MeetingRoom>> action) => action(theRooms));

    shell.GetRooms();

    Assert.AreEqual(theRooms, shell.Rooms);
}
  • 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-20T09:50:42+00:00Added an answer on May 20, 2026 at 9:50 am

    Here is some pseudo code, I haven’t run it. But I think that’s what you want.

    SetupCallback is what you are interested in.

    For all the calls to _meetingRoomServiceFake.GetRooms, simply set the _getRoomsCallback to the parameter passed in.

    You now have a reference to the callback that you are passing in your viewmodel, and you can call it with whatever list of MeetingRooms you want to test it.
    So you can test your asynchronous code almost the same way as synchronous code. it’s just a bit more ceremony to setup the fake.

    Action<List<MeetingRoom>> _getRoomsCallback = null;
    IMeetingRoomService _meetingRoomServiceFake;
    
    
    private void SetupCallback()
    {
         Mock.Get(_meetingRoomServiceFake)
             .Setup(f => f.GetRooms(It.IsAny<Action<List<MeetingRoom>>>()))
             .Callback((Action<List<MeetingRoom>> cb) => _getRoomsCallback= cb);
    }
    
    [Setup]
    public void Setup()
    {
         _meetingRoomServiceFake = Mock.Of<IMeetingRoomService>();
         SetupCallback();
    }
    
    [Test]
    public void Test()
    {
    
          var viewModel = new SomeViewModel(_meetingRoomServiceFake)
    
          //in there the mock gets called and sets the _getRoomsCallback field.
          viewModel.GetRooms();
          var theRooms = new List<MeetingRoom>
                       {
                           new MeetingRoom(1, "some room"),
                           new MeetingRoom(2, "some other room"),
                       };
    
         //this will call whatever was passed as callback in your viewModel.
         _getRoomsCallback(theRooms);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using .net unit testing in my project. I can unit test, get
I am using .net unit testing in my project. I would like to know
Unit testing and ASP.NET web applications are an ambiguous point in my group. More
Unit testing is, roughly speaking, testing bits of your code in isolation with test
Unit testing sounds great to me, but I'm not sure I should spend any
Unit testing with C/C++: What do you teach people who either did not do
I've heard that unit testing is totally awesome, really cool and all manner of
Does Python have a unit testing framework compatible with the standard xUnit style of
So I know that unit testing is a must. I get the idea that
How are people unit testing their business applications? I've seen a lot of examples

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.