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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:15:54+00:00 2026-06-04T03:15:54+00:00

I have the below method: public void Enqueue(ICommand itemToQueue) { if (itemToQueue == null)

  • 0

I have the below method:

public void Enqueue(ICommand itemToQueue)
{
    if (itemToQueue == null)
    {
        throw new ArgumentNullException("itemToQueue");
    }

    // Using the dynamic keywork to ensure the type passed in to the generic
    // method is the implementation type; not the interface.
    QueueStorage.AddToQueue((dynamic)itemToQueue);
}

With QueueStorage being a dependency that implements IQueueStorage. I wish to unit test it but the (dynamic) keyword seems to be blocking Moq from binding correctly to it. The keyword is used to correctly assign the concrete class type rather than ICommand interface type when it is added to the queue.

The unit test looks like this:

[Test]
public void Enqueue_ItemGiven_AddToQueueCalledOnQueueStorage()
{
    int timesAddToQueueCalled = 0;

    var dummyQueueStorage = new Mock<IQueueStorage>();
    var testCommand = new TestCommand();

    var queueManager = new AzureCommandQueueManager();

    dummyQueueStorage
        .Setup(x => x.AddToQueue(It.IsAny<TestCommand>()))
        .Callback(() => timesAddToQueueCalled++);

    queueManager.QueueStorage = dummyQueueStorage.Object;

    queueManager.Enqueue(testCommand);

    Assert.AreEqual(1, timesAddToQueueCalled);
}

Whilst test command is a blank implementation of ICommand:

private class TestCommand : ICommand
{
}

public interface ICommand
{
}

The timesAddedToQueuCalled is not being incremented. I’ve tried using It.IsAny<ICommand> and (testCommand) to no avail. It looks like the Callback method is not being executed. Can anyone see what I’m doing wrong?

EDIT: IQueueStorage code:

public interface IQueueStorage
{
    void AddToQueue<T>(T item) where T : class;

    T ReadFromQueue<T>() where T : class;
}
  • 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-06-04T03:15:56+00:00Added an answer on June 4, 2026 at 3:15 am

    Here is code which works without problems:

    public class AzureCommandQueueManager
    {
        public void Enqueue(ICommand itemToQueue)
        {
            if (itemToQueue == null)            
                throw new ArgumentNullException("itemToQueue");
    
            QueueStorage.AddToQueue((dynamic)itemToQueue);
        }
    
        public IQueueStorage QueueStorage { get; set; }
    }
    
    public interface IQueueStorage
    {
        void AddToQueue<T>(T command) where T : class;        
    }
    
    public class TestCommand : ICommand  {}
    
    public interface ICommand {}
    

    And test method:

    [Test]
    public void Enqueue_ItemGiven_AddToQueueCalledOnQueueStorage()
    {
        int timesAddToQueueCalled = 0;
    
        var dummyQueueStorage = new Mock<IQueueStorage>();
        var testCommand = new TestCommand();
    
        var queueManager = new AzureCommandQueueManager();
    
        dummyQueueStorage
            .Setup(x => x.AddToQueue(It.IsAny<TestCommand>()))
            .Callback(() => timesAddToQueueCalled++);
    
        queueManager.QueueStorage = dummyQueueStorage.Object;
    
        queueManager.Enqueue(testCommand);
    
        Assert.AreEqual(1, timesAddToQueueCalled);
    }
    

    The only difference I see – you have private modifier of TestCommand class. Btw if it is private, how do you access that class from your tests?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method as below: ClassA.java @Transactional public void methodA(){ ExecutorService executorService =
I have the below method: public String tryGoogleAuthentication(String auth_token){ ContactsService contactsService = new ContactsService(.....);
I have below example of abstract method patter. public interface Product { public void
I have the classes below: SuppliersRepository.cs (with the interface defining the method): public class
I have a generic method define as below public T MyMethod<T>(extra params) My method
I have below method to save an object to database using Hibernate but I
I have a search method below using linq, my search citeria is based on
Lets say I have an interface passed to my method: public void AlphaToChar(iList _blah)
I have added an action to itemMenu using the setAction method below, but when
I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)

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.