8

I have to convert MS unit tests to NUnit and came across this assertion.

Assert.AreEqual(collection.Select(item => item.location.id).Distinct().Count(), 1);

I was hoping the there would be an elegant way to write that with constraints but I have not been able to find one. My solution is this, but Im not that happy with it:

Expect(collection.Select(item => item.location.id).Distinct().Count(), Is.EqualTo(1));

Is there a better way of writing that assertion where the intent is clearer readable? (Using Has. or Map(collection). )


Edit 2:

I just realized it may be helpful to clearly state what the intent is:

all items in the collection have the same location ID (without knowing what that ID is)


Edit 1:

This is what the collection may look like as JSON:

[{itemId=1, location={name="A", id=1}},
 {itemId=2, location={name="A", id=1}},
 {itemId=3, location={name="A", id=1}}]

distinct.count = 1     => pass

[{itemId=1, location={name="A", id=1}},
 {itemId=2, location={name="A", id=1}},
 {itemId=4, location={name="B", id=2}}]

distinct.count = 2     => fail

Edit 3: my final solution, based on Fabio's answer

IEnumerable<long?> locationIds = collection.Select(item => item.location.id);
Expect(locationIds, Has.All.EqualTo(locationIds.FirstOrDefault()));
7
  • You are expecting all items to be unique? Can you post an example of what the collection may look like?
    – Dr Schizo
    Aug 9, 2016 at 13:10
  • I'm trying to understand the value of this assert, calling .Distinct will automatically strip away all duplicates for you, and your result will always be equal to 1. Aug 9, 2016 at 14:05
  • @MoeGhafari good spot. In fact the only way that will fail is if the list is empty as the condition will always hold true.
    – Dr Schizo
    Aug 9, 2016 at 14:27
  • @MoeGhafari I just added what the collection may look like; this is to assure that all items are in the same location
    – Holly
    Aug 9, 2016 at 14:27
  • @DrSchizo In my understanding Select(item => item.location.id) projects the location IDs of each item as a new list, Distinct() will kick all duplicates and count will then return the number of unique location IDs in the collection
    – Holly
    Aug 9, 2016 at 14:38

3 Answers 3

13

Readable version

int expectedCount = 1;
int actualCount = collection.Select(item => item.location.id)
                            .Distinct()
                            .Count()

Assert.AreEqual(expectedCount, actualCount);

I am not sure but you can try this version, where phrase "Is all equal to..." must help to "non-programmers" and your code get rid of "magic" numbers

var value = collection.Select(item => item.location.id).FirstOrDefault();
Assert.That(collection.Select(item => item.location.id), Is.All.EqualTo(value));
6
  • sorry, but to me that is not any better; I was hoping to use the NUnit constraints API not just reformat that lambda epression, I would like something that a none-programmer can read and understand
    – Holly
    Aug 9, 2016 at 13:00
  • Seems as though the above is the simplest. See answer from Mark Seeman stackoverflow.com/a/32936435/1134076
    – Dr Schizo
    Aug 9, 2016 at 13:19
  • @Holly, Is.All.EqualTo is another way to assert that collection contains items with same location.id. There are Has.Exact constraint can be used for the count of distinct values
    – Fabio
    Aug 9, 2016 at 14:24
  • @Fabio well those two were my first guesses and this is kinda what I want; the problem is, I do not know the value the location ID needs to be equal to, only that all location IDs have to be the same
    – Holly
    Aug 9, 2016 at 14:31
  • 1
    @Fabio I guess your second answer is about as close as I can get, although I dislike doing the same projection twice, so I changed it a bit - see my 3rd Edit - thanks
    – Holly
    Aug 10, 2016 at 8:53
8

If I've understood what you want to do... this should do it...

Assert.That(collection.Select(() => item.location.id), Is.Unique);

Leaving this wrong answer here... somebody may want to test uniqueness, but that's not what this guy wanted!!!

5
  • 2
    Oh wait! I hadn't understood. You want the opposite of unique. :-)
    – Charlie
    Aug 9, 2016 at 15:57
  • It's actually a gap in the fluent syntax, although it's not something anyone has asked for over the years so may not be a big deal. Feel free to request a feature.
    – Charlie
    Aug 9, 2016 at 16:08
  • I also need this feature. Is 'Is.All.Not.Unique' wrong?
    – Denis535
    Jun 30, 2017 at 8:47
  • This is what I was looking for today ;)
    – scar80
    May 30, 2018 at 8:40
  • Exactly what I wanted! :) Aug 31, 2023 at 6:34
0

Based upon the data in your example, noting that name is equal where Id is equal, you could use:

var distinctLocations = collection.select(item => item.location).Distinct();

Assert.That(distinctLocations, Has.Exactly(1).Items);

If that assumption is incorrect, you would of course need to extract just the id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.