Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get arguments passed to a data access layer that uses System.Action as an input parameter?
    primarykey
    data
    text
    <p>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.</p> <p>The method I'm testing calls the DataService class' Create method.</p> <p><strong>Calling Create Method</strong></p> <pre><code>var contactProductLink = this.dsService.Create&lt;ContactProductLink&gt;(x =&gt; { 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; }); </code></pre> <p><strong>DataService Create Method:</strong></p> <pre><code>public TEntity Create&lt;TEntity&gt;(Action&lt;TEntity&gt; propertySetter = null) where TEntity : class { var tEntity = this.Context.Create&lt;TEntity&gt;(); if (propertySetter != null) { propertySetter(tEntity); } return tEntity; } </code></pre> <p>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:</p> <pre><code>mockDataSupplierService.Received().Create&lt;ContactProductLink&gt;(Arg.Any&lt;Action&lt;ContactProductLink&gt;&gt;()); </code></pre> <p>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).</p> <p>So after all of that what I'm asking is:</p> <ol> <li>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?</li> <li>Is there a better way to do this than what I'm currently trying to do?</li> </ol> <p><strong>Solution</strong> </p> <pre><code>//Arrange mockDataSupplierService.Create&lt;ContactProductLink&gt;(Arg.Do&lt;Action&lt;ContactProductLink&gt;&gt;(x=&gt; actionToPopulateEntity = x)); //Assert mockDataSupplierService.Received().Create&lt;ContactProductLink&gt;(Arg.Any&lt;Action&lt;ContactProductLink&gt;&gt;()); var entity = new ContactProductLink(); actionToPopulateEntity.Invoke(entity); Assert.AreEqual(ExpectedContactId, entity.ContactId); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload