views:

209

answers:

5

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>.

+1  A: 

You could shorten it a tad by using the Assert.AreEqual method instead:

Assert.AreEqual(domain.RenderContent().OfType<VerifyRequest>().Count(), 0);
David Morton
Yeah, but doesn't NUnit have a built in syntax-helper for this?
goofballLogic
+11  A: 

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.

Mark Seemann
+2  A: 

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.

Marco Spatz
Perfect. I knew they had to have it somewhere.
goofballLogic
+1  A: 

I prefer the Assert.AreEqual approach; NUNit uses Assert.That for the internal Assert, STringAssert, etc objects. I like just doing Assert.AreEqual(0, domain.RenderContent().OfType().Count()); to check for the counts.

This way, it checks directly that no objects of a type have any number of records, but to a point the variations you see are preference and they all are equally valid. You have to choose what you like for your style of development.

Brian
+1  A: 

The Any extension method, which can be given a lambda expression:

Assert.IsFalse(domain.RenderContent().Any(i => i is VerifyRequest));
Kristof Verbiest