ユニットテストのためにnunitおよびautofixtureを使用しているときに非常に奇妙な例外が発生しました。Autofixture - GuardClauseException
IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());
var assertion = new GuardClauseAssertion(fixture);
:
は、私はすべての入力などのオブジェクトを取得し、(私はJSONにオブジェクトをフォーマットし、要求を作成しています)私のユニットテストで
を私はこれを行うそれらのオブジェクトに応じてのHttpRequestをやっ異なるクラスを持っています私はこれを行うすべての私のクラスと続い
:
assertion.Verify(typeof(MyClass));
すべてのクラスになりましたテストが、1つはないに合格するまで。テストは例外
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.
をスローそして、これは、内部例外です:
if(string.IsNullOrEmpty(myObject.Name))
throw new InvalidOperationException("...");
にmentionted方法:次のようになります私のクラスの最後のメソッドで
----> 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.
内部例外は です(ステータスは列挙型ですが、列挙型ではない他のオブジェクトでエラーが発生しました)
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);
}
私は、オブジェクトの同じ型を持つ他のすべてのクラスで同じif節を実行して渡すので、私は疑問に思っています。
(これは、本試験と同じだ:)
assertion.Verify(typeof(myObject).GetMethods());
私は理由何ができるかわかりません。
問題を再現するために使用できる[mcve]を提供してください。 – Nkosi
エラーメッセージには何が表示されますか? –
@ MarkSeeman erororメッセージsais "Guard句のテストは、遅延イテレータブロックを含む可能性のあるメソッドに対して実行されましたが、テストは失敗しました。"しかし、これのための解決策は私を助けなかった。私は単体テストの新しいので、それは解決しやすい質問を申し訳ありません。 PS:申し訳ありませんが、私は例外メッセージを忘れました – Nick77