2017-08-15 9 views
0

ユニットテストのために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()); 

私は理由何ができるかわかりません。

+3

問題を再現するために使用できる[mcve]を提供してください。 – Nkosi

+0

エラーメッセージには何が表示されますか? –

+0

@ MarkSeeman erororメッセージsais "Guard句のテストは、遅延イテレータブロックを含む可能性のあるメソッドに対して実行されましたが、テストは失敗しました。"しかし、これのための解決策は私を助けなかった。私は単体テストの新しいので、それは解決しやすい質問を申し訳ありません。 PS:申し訳ありませんが、私は例外メッセージを忘れました – Nick77

答えて

0

私のクラスは今テストに合格しました。

申し訳ありませんが、MCVを提供できませんでしたが、問題を再現できませんでした。

私は句場合、一部を削除し、いくつかを追加し、いくつかの点で私はChangeStatus(の私の列挙型のステータスにnullチェックを追加しました)

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

これは、エラーを解決しました。私はその質問をする前に同じことを試みたと思った。

ご協力いただきありがとうございます。

関連する問題