代码之家  ›  专栏  ›  技术社区  ›  Nick77

Autofixture-GuardClauseseException

  •  0
  • Nick77  · 技术社区  · 7 年前

    我有不同的类,它们都将对象作为输入,并根据这些对象执行httprequest(我将对象格式化为json并发出请求)

    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("...");
    

    内部异常中提到的方法是: (状态是一个枚举,但我在其他不是枚举的对象上遇到了错误)

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

    我不知道原因是什么。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Nick77    7 年前

    我的班现在通过了考试。

    很抱歉,我没有/无法提供任何MCV,但我根本无法/无法再现该问题。

    我删除了一些if子句并添加了一些,在某个时候,我在ChangeStatus()中为我的枚举状态添加了一个null检查

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