2017-08-15 35 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(); 

这解决了错误。在提出问题之前,我想我也一样。

感谢每一个帮助和您的时间。