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.