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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:56:37+00:00 2026-06-16T20:56:37+00:00

I’m trying to create some unit tests for an application I’ve recently inherited. Currently

  • 0

I’m trying to create some unit tests for an application I’ve recently inherited. Currently using NSubstitute because that’s what the previous programmer used, but I’m not attached to it.

The method I’m testing calls the DataService class’ Create method.

Calling Create Method

var contactProductLink = this.dsService.Create<ContactProductLink>(x =>
{
    x.ContactRoleId = prod.RoleId;
    x.ContactId = contactViewModel.ContactId;
    x.ProductId = prod.ProductId;
    x.Active = true;
    x.InsertDate = DateTime.Now;
    x.InsertUserId = user.employeeId;
    x.UpdateDate = DateTime.Now;
    x.UpdateUserId = user.employeeId;
});

DataService Create Method:

public TEntity Create<TEntity>(Action<TEntity> propertySetter = null) where TEntity : class
{
    var tEntity = this.Context.Create<TEntity>();
    if (propertySetter != null)
    {
        propertySetter(tEntity);
    }

    return tEntity;
}

The approach I’ve taken (and maybe there’s a better way) is to use NSubstitute to mock the DataService. When I’m doing my assertions at the end, I’m checking to make sure that the Create method was called:

mockDataSupplierService.Received().Create<ContactProductLink>(Arg.Any<Action<ContactProductLink>>());

However, I’d like to also verify the input that was sent to the method is correct, and here’s where I’m running into trouble. I can get the System.Action object that was passed to the Create method, but I can’t figure out how to pull out the parameters (such as ContactRoleId, ContactId, etc. as posted in the calling create method code snippet).

So after all of that what I’m asking is:

  1. How can I access those input parameters so I can verify the correct arguments are being passed to the data service? Is it even possible?
  2. Is there a better way to do this than what I’m currently trying to do?

Solution

//Arrange
mockDataSupplierService.Create<ContactProductLink>(Arg.Do<Action<ContactProductLink>>(x=> actionToPopulateEntity = x));

//Assert
mockDataSupplierService.Received().Create<ContactProductLink>(Arg.Any<Action<ContactProductLink>>());
var entity = new ContactProductLink();
actionToPopulateEntity.Invoke(entity);
Assert.AreEqual(ExpectedContactId, entity.ContactId);
  • 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-16T20:56:38+00:00Added an answer on June 16, 2026 at 8:56 pm

    How can I access those input parameters so I can verify the correct arguments are being passed to the data service? Is it even possible?

    Essentially you can’t, as it is not possible to extract “code” details from action (consider what happens when you pass an action that doesn’t set any properties – this is totally legal, but would break hypothetical mechanism).

    However, you can try this instead:

    1. Create entity with initial values
    2. Use Arg.Invoke argument, telling NSubstitute to use chosen object as action parameter
    3. Verify that entity properties values changed

    For example:

    // Arrange
    var entity = new ContactProductLink
    {
        ContactRoleId = // ...
        // ...
    };
    
    mockDataSupplierService
        .Create<ContactProductLink>(Arg<ContactProductLink>.Invoke(entity));
    
    // Act
    // ...
    
    Assert.That(entity.ContactRoleId, Is.EqualTo(2));
    // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.