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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:53:56+00:00 2026-05-23T00:53:56+00:00

So the scenario is this: a user does some action (like earn a badge

  • 0

So the scenario is this: a user does some action (like earn a badge or unlock something) and an email notification gets sent out. One to the user (with a message like “You’ve unlocked XYZ…”) and then a different message to each of their friends like (“You’re friend has unlocked XYZ…”).

public interface INotify
{
   void Notify(User user, User friend);
}

public class NotificationService
{
    private IEnumerable<INotify> _notifiers;

    public NotificationService(IEnumerable<INotify> notifiers)
    {
        _notifiers = notifiers;
    }

    public SendNotifications()
    {
        User user = GetUser();
        IEnumerable<User> friends = GetFriends();

        foreach(var notifier in _notifiers)
        {
            //Send notification to user
            notifier.Notify(user, null);

            //send notification to users friends
            foreach(var friend in friends)
                notifier.Notify(user, friend);
        }
    }
}

I’m trying to use moq to test that each notifier is called 2x. Once passing null as the second parameter and the second time passing in a value to both parameters.

[Test]
public void MakeSureEveryoneIsNotified()
{
     var notifierMock = new Mock<INotifier>();

     var svc = new NotificationService(new List<INotifier>{ notifierMock.Object });    
     svc.SendNotifications();

     notifierMock.Verify(x => x.Notify(It.Is<User>(user => user.UserId == 1), null), Times.Once());
     notifierMock.Verify(x => x.Notify(It.Is<User>(user => user.UserId == 1), It.Is<User>(user => user.UserId == 2)), Times.Once());
}

The problem is that the second verify call throws an ArgumentNullException for the second parameter. Is there away to say “Check the first call has these parameters, and then the second call has other parameters”. I know I can get it around it simply by calling:

notifierMock.Verify(x => x.Notify(It.IsAny<User>(), It.IsAny<User>()), Times.Exactly(2));

But I was wanting to be a little more specific. Anyway to do this?

  • 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-23T00:53:56+00:00Added an answer on May 23, 2026 at 12:53 am

    You can achieve this by recording what happens on each call to Notify. Then you can compare the recording to what’s expected:

    [TestMethod]
    public void TestMoqInvocations()
    {
        var notifierMock = new Mock<INotifier>();
    
        var svc = new NotificationService(new List<INotifier>{ notifierMock.Object });    
        svc.SendNotifications();
    
        var invocations = new List<NotifyParams>();
    
        notifierMock
            .Setup(f => f.Notify(It.IsAny<User>(), It.IsAny<User>()))
            .Callback<string, string>((user, friend) => invocations.Add(new NotifyParams{user = user, friend = friend}));
    
        Assert.AreEqual(1, invocations[0].user.UserId);
        Assert.IsNull(invocations[0].friend);
        Assert.AreEqual(1, invocations[1].user.UserId);
        Assert.AreEqual(2, invocations[1].user.UserId);
    }
    
    public struct NotifyParams { 
        public User user {get;set;}
        public User friend { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the scenario: if this is a user's first time logging into my web
This is the standard scenario: User registers on the site User receives an account
Here is my scenario. User fills out this large page which is dynamically created
The scenario is this. A web page contains: some DIVs whose visibility can be
i'm struggling around for days now ...this is the scenario: a user can upload
In my Django app, I need to implement this timer-based functionality: User creates some
here is the scenario 1: user starts to type some word, autocomplete engine shows
Are there any security exploits that could occur in this scenario: eval(repr(unsanitized_user_input), {__builtins__: None},
Consider this scenario: I've an XML file called person.xml with the following data in
Consider this scenario. I have my own website, that I use as my identifier,

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.