Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit: What is the most concise way to assert whether an IEnumerable contains an object of a certain type?

I have a method named RenderContent which returns object[]
In my unit test, I need to assert that this array does not contain any objects of type VerifyRequest

At the moment, I'm using the following Assert statement. Is there anything more concise?

Assert.That(
    domain.RenderContent().OfType<VerifyRequest>().Count(),
    Is.EqualTo(0)
);

I prefer to use fluent syntax. Note also that RenderContent returns object[], not IQueryable<object>.

like image 338
goofballLogic Avatar asked Mar 01 '10 13:03

goofballLogic


2 Answers

If you are using NUnit 2.5, you could use something like:

Assert.That(domain.RenderContent(), Has.None.InstanceOf<VerifyRequest>());

But I'm not sure if other unit test frameworks support this assert-style.

like image 153
Marco Spatz Avatar answered Oct 18 '22 11:10

Marco Spatz


Although I don't know the exact NUnit syntax for IsFalse assertion, the best fit for this kind of test is the Any extension method:

Assert.IsFalse(domain.RenderContent().OfType<VerifyRequest>().Any());  

It might be tempting to use the Count method, but Any is more efficient, as it will break on the first occurrence.

like image 44
Mark Seemann Avatar answered Oct 18 '22 11:10

Mark Seemann