0

I encountered a very strange exception while working with nunit and autofixture for unit testing.

I have different classes which all get objects as input and doing httprequest depending on those objects ( I'm formatting the objects to json and make requests )

In my unit test I do this:

IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());
var assertion = new GuardClauseAssertion(fixture);

Then with all my classes I do this:

assertion.Verify(typeof(MyClass));

Every class until now passed the test but one not. The test throws the exception

Message: Ploeh.AutoFixture.Idioms.GuardClauseException : A Guard Clause test was performed
on a method that may contain a deferred iterator block, but the test failed. See the inner
exception for more details. However, because of the deferred nature of the iterator block,
this test failure may look like a false positive. Perhaps you already have a Guard Clause
in place, but in conjunction with the 'yield' keyword (if you're using C#); if this is the
case, the Guard Clause is dormant, and will first be triggered when a client starts looping
over the iterator. This doesn't adhere to the Fail Fast principle, so should be addressed.

And this is the inner exception:

----> Ploeh.AutoFixture.Idioms.GuardClauseException : An attempt was made to assign the
value null to the parameter "status" of the method "ChangeStatus", and no Guard Clause
prevented this. Are you missing a Guard Clause?
Method Signature: System.String ChangeStatus(Interfaces.IProject, Status)
Parameter Type: Status, , Version=1.0.0.0
Culture=neutral, PublicKeyToken=bd4b9bc26bc147ff
Declaring Type: ReleaseRepository, 
Version=1.0.0.0, Culture=neutral, PublicKeyToken=bd4b9bc26bc147ff
Reflected Type: ReleaseRepository
Version=1.0.0.0, Culture=neutral, PublicKeyToken=bd4b9bc26bc147ff
  ----> System.InvalidOperationException : The passed project has no valid name.

in the last method of my class which looks like this:

if(string.IsNullOrEmpty(myObject.Name))
     throw new InvalidOperationException("...");

The Method mentionted in the inner exception is this: (Status is an enum, but I got the error with other objects which wasnt enums)

public string ChangeStatus(IObject1, IObject2, Status status)
{
// Here are the if clauses to check if something is null or not given
return client.Post(url, status);
}

I'm wondering because I do the same if clause in all my other classes with the same type of object and they pass.

(It's the same with this test:)

assertion.Verify(typeof(myObject).GetMethods());

I have no idea what could be the reason.

Nick77
  • 23
  • 1
  • 12
  • 3
    Provide a [mcve] that can be used to reproduce the problem. – Nkosi Aug 15 '17 at 12:06
  • What does the error message say? – Mark Seemann Aug 15 '17 at 12:31
  • @MarkSeeman the eroror message sais "A Guard Clause test was performed on a method that may contain a deferred iterator block, but the test failed." But the solutions for this on other post didn't helped me. Im a new to unit testing, so sorry if its an easy to solve question. PS: sorry that i forgot exception message – Nick77 Aug 15 '17 at 12:34
  • 1
    What more does the error message say? Please post the entire exception message, including any inner exceptions, if present. Please edit your question in order to do that, instead of posting it in the comments. – Mark Seemann Aug 15 '17 at 12:40
  • 1
    Could you include the code for `ChangeStatus`? – mjwills Aug 15 '17 at 13:04
  • 1
    Cannot reproduce because code as given doesn't compile. I second @Nkosi's comment: please provide an MCV. – Mark Seemann Aug 15 '17 at 15:20
  • How does `MyClass` looks like? – Nikos Baxevanis Aug 17 '17 at 08:35
  • My class is just a collection of methods which all call a http client – Nick77 Aug 18 '17 at 05:26

1 Answers1

0

My class passed the test now.

Sorry I didn't / couldn't gave any MCV but I couldn't / can't reproduce the problem at all.

I deleted some if clauses and added some and at some point I added a null check for my enum status in ChangeStatus()

public string changeStatus(IObject1, IObject2, Status status)
if(status == null)
   throw new Exception();

this solved the error. I thought I tried the same before asking the question.

Thanks for every help and your time.

Nick77
  • 23
  • 1
  • 12