Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to unit test this code with private implementation?
    primarykey
    data
    text
    <p>I know there are questions are are similar to this, but none seem to satisfy the correct way to address the following code snippet in regards to unit testing</p> <pre><code>public class InMemoryQueue : ICloudQueue { private Queue&lt;object&gt; _inMemoryCollection = new Queue&lt;object&gt;(); private readonly ServiceBusQueueSettings _settings; public InMemoryQueue(ServiceBusQueueSettings settings = null) { _settings = settings; if (_settings == null) _settings = new ServiceBusQueueSettings(); } public async Task&lt;IEnumerable&lt;T&gt;&gt; ReceieveAsync&lt;T&gt;(int batchCount) { List&lt;T&gt; result = new List&lt;T&gt;(); for (int i = 0; i &lt; batchCount; i++) { _inMemoryCollection.Dequeue(); } return result; } public async Task AddToAsync&lt;T&gt;(IEnumerable&lt;T&gt; items) { items.ToList().ForEach(x=&gt;_inMemoryCollection.Enqueue(x)); } } </code></pre> <p>OK, I want to unit this this implementation of ICloudQueue to ensure that items that are added to via AddToAsync are held in memory, and any call to RecieveAsync is pulled from memory.</p> <p>However - the complication is that the data is held in _inMemoryCollection which is a private variable. If the _inMemoryCollection queue WASNT private, one of the tests might look like this :</p> <pre><code> [Test] public async Task Adding_To_Queue_Results_In_Message_Added_To_List() { //arrange var listToAdd = new List&lt;object&gt;() { new object() }; var inMemoryQueue = new InMemoryQueue(); //act await inMemoryQueue.AddToAsync(listToAdd); //assert Assert.IsTrue(inMemoryQueue._inMemoryCollection.Count == 1); } </code></pre> <p>I guess I could change the _inMemoryCollection to be internal, then use the [InternalsVisibleTo] attribute to expose it to the unit tests. Although I'm reluctant as it means changing the implementation just to satisfy unit tests.</p> <p>Is this my only option?</p>
    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.
 

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