Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Mock setup for nullable types
    primarykey
    data
    text
    <p>I have an interface which has nullable parameters like this</p> <pre><code>Result&lt;Notice&gt; List(int offset, int limit, Guid? publicationId, Guid? profileId, DateTime? toDate, ListingOrder order); </code></pre> <p>This is how I attempted to mock this this method</p> <pre><code>mockNoticesClient.Setup(c =&gt; c.List(It.IsAny&lt;int&gt;(), It.IsAny&lt;int&gt;(), It.IsAny&lt;Nullable&lt;Guid&gt;&gt;(), It.IsAny&lt;Nullable&lt;Guid&gt;&gt;(), It.IsAny&lt;Nullable&lt;DateTime&gt;&gt;(), Data.Notices.ListingOrder.DateDesc)).Returns(dataNotices); </code></pre> <p>Then when trying to use the method</p> <pre><code>var results = this.noticesClient.List(0, 100, null, profileId, latestNoticeTime, Data.Notices.ListingOrder.DateDesc); </code></pre> <p>Whenever this line is run though this exception is thrown</p> <pre><code>... threw an exception of type 'System.NullReferenceException' ... {System.NullReferenceException} </code></pre> <p>I have tried a few different combinations like using setup with null in the parameter but this doesn't work either. I am using Moq 4.0.10827 which is the latest version (at present).</p> <p><strong>Edit:</strong> The constructor for the noticesClient takes the interface for the dataNoticesClient</p> <pre><code>public Client(Data.Notices.INotices noticesClient) </code></pre> <p>and initalised like this </p> <pre><code>mockNoticesClient = new Mock&lt;Data.Notices.INotices&gt;(); noticesClient = new Client(mockNoticesClient.Object); mockNoticesClient.Setup(c =&gt; c.List(It.IsAny&lt;int&gt;(), It.IsAny&lt;int&gt;(), It.IsAny&lt;Nullable&lt;Guid&gt;&gt;(), It.IsAny&lt;Nullable&lt;Guid&gt;&gt;(), It.IsAny&lt;Nullable&lt;DateTime&gt;&gt;(), It.IsAny&lt;Data.Notices.ListingOrder&gt;())).Returns(dataNotices); mockNoticesClient.Setup(c =&gt; c.List(It.IsAny&lt;int&gt;(), It.IsAny&lt;int&gt;(), It.IsAny&lt;Guid?&gt;(), It.IsAny&lt;Guid?&gt;(), It.IsAny&lt;DateTime?&gt;(), It.IsAny&lt;Data.Notices.ListingOrder&gt;())).Returns(dataNotices); </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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