Autofixture - GuardClauseException
Autofixture - GuardClauseException
我在使用 nunit 和 autofixture 进行单元测试时遇到了一个非常奇怪的异常。
我有不同的 classes,它们都将对象作为输入并根据这些对象执行 httprequest(我将对象格式化为 json 并发出请求)
在我的单元测试中我这样做:
IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());
var assertion = new GuardClauseAssertion(fixture);
然后用我所有的 classes 我这样做:
assertion.Verify(typeof(MyClass));
到目前为止,每个 class 都通过了测试,但没有一个。测试抛出异常
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.
在我的 class 的最后一个方法中,它看起来像这样:
if(string.IsNullOrEmpty(myObject.Name))
throw new InvalidOperationException("...");
内部异常中提到的方法是这样的:
(状态是一个枚举,但我得到了其他不是枚举的对象的错误)
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);
}
我想知道,因为我在所有其他 class 中使用相同类型的对象执行相同的 if 子句并且它们通过了。
(本次测试也一样:)
assertion.Verify(typeof(myObject).GetMethods());
我不知道是什么原因。
我的class现在通过了测试。
抱歉,我没有/不能提供任何 MCV,但我根本不能/不能重现问题。
我删除了一些 if 子句并添加了一些,有时我在 ChangeStatus() 中为我的枚举状态添加了空检查
public string changeStatus(IObject1, IObject2, Status status)
if(status == null)
throw new Exception();
这解决了错误。我想我在问这个问题之前也试过了。
感谢您的帮助和您的宝贵时间。
我在使用 nunit 和 autofixture 进行单元测试时遇到了一个非常奇怪的异常。
我有不同的 classes,它们都将对象作为输入并根据这些对象执行 httprequest(我将对象格式化为 json 并发出请求)
在我的单元测试中我这样做:
IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());
var assertion = new GuardClauseAssertion(fixture);
然后用我所有的 classes 我这样做:
assertion.Verify(typeof(MyClass));
到目前为止,每个 class 都通过了测试,但没有一个。测试抛出异常
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.
在我的 class 的最后一个方法中,它看起来像这样:
if(string.IsNullOrEmpty(myObject.Name))
throw new InvalidOperationException("...");
内部异常中提到的方法是这样的: (状态是一个枚举,但我得到了其他不是枚举的对象的错误)
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);
}
我想知道,因为我在所有其他 class 中使用相同类型的对象执行相同的 if 子句并且它们通过了。
(本次测试也一样:)
assertion.Verify(typeof(myObject).GetMethods());
我不知道是什么原因。
我的class现在通过了测试。
抱歉,我没有/不能提供任何 MCV,但我根本不能/不能重现问题。
我删除了一些 if 子句并添加了一些,有时我在 ChangeStatus() 中为我的枚举状态添加了空检查
public string changeStatus(IObject1, IObject2, Status status)
if(status == null)
throw new Exception();
这解决了错误。我想我在问这个问题之前也试过了。
感谢您的帮助和您的宝贵时间。