Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a NUnit constraint meaning "{collection} does not contain {item}"

Tags:

c#

nunit

I'm struggling to make an assertion about the absence of a particular item in an enumeration. Specifically, this is what my test looks like:

// Take an item from a queue of scheduled items...
ItemQueue pendingQueue = schedule.PendingItems; // PendingItems is an IEnumerable<int>
int item = pendingQueue.FirstItem;

// ...process the item...
processor.DoSomethingWith(item);

// ...and the schedule must not contain the item anymore:
Assert.That(schedule.PendingItems, Does.Not.Contain(item));

Of course, Does.Not.Contain is not a valid nUnit constraint. How can I express it in a valid fluent syntax?

like image 287
Humberto Avatar asked Sep 27 '10 16:09

Humberto


2 Answers

Assert.That(schedule.PendingItems, Has.No.Member(item))

Only with NUnit 2.4 / 2.5

like image 183
gcores Avatar answered Nov 15 '22 05:11

gcores


Use the CollectionAssert method:

CollectionAssert.DoesNotContain(schedule.PendingItems, item);
like image 19
Rok Strniša Avatar answered Nov 15 '22 06:11

Rok Strniša