2017-08-15 30 views
0

我遇到了一個很奇怪的例外與NUnit的和autofixture工作進行單元測試時。Autofixture - GuardClauseException

我有不同類別,所有獲得的對象作爲輸入,並根據這些對象(我格式化對象以JSON和提出要求)

在我的單元測試我做這做的HttpRequest:

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

然後用我所有的課我這樣做:

assertion.Verify(typeof(MyClass)); 

每次上課到現在爲止通過了測試,但人們不。該測試拋出異常

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. 

這是內部異常:

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

在我的課的最後一個方法,看起來像這樣:

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

在mentionted的方法內部異常是: (狀態爲枚舉,但我與wasnt枚舉其他對象得到了錯誤)

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); 
} 

我不知道,因爲我做同樣的,如果子句中我的所有其他類具有相同類型的對象,他們通過。

(這是與這個測試:)

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

我不知道可能是什麼原因相同。

+3

提供一個[MCVE]可用於重現該問題。 – Nkosi

+0

錯誤消息說什麼? –

+0

@MarkSeeman eroror消息sais「對一個可能包含延遲迭代器塊的方法執行Guard子句測試,但測試失敗。」但在其他帖子上的解決方案並沒有幫助我。我是一個新的單元測試,所以很抱歉,如果它是一個容易解決的問題。 PS:對不起,我忘記了異常信息 – Nick77

回答

0

我的課程現在通過了測試。

對不起,我沒有/不能給任何MCV,但我不能/不能在所有重現該問題。

if引導的從句我刪除了一些,並增加了一些,在某些時候我在ChangeStatus添加一個空檢查我的枚舉狀態()

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

這解決了錯誤。在提出問題之前,我想我也一樣。

感謝每一個幫助和您的時間。