【问题标题】:Nsubstitute return different based on the object propertiesNsubstitute 根据对象属性返回不同
【发布时间】:2021-09-10 10:28:08
【问题描述】:

我有如下代码。 第一次调用 save(),我希望它抛出异常。 而当 obj.field="ok" 时的第二次调用,我不希望它抛出异常。 我如何通过 Nsubstitute 实现这一目标?

Try { //Source codes
    workItem.save() //save() has void as return type
} catch (Exception e) {
    if (somecondition){
        workItem.field = "OK";
        workItem.save();
    }
}

// Unit Test
workItem.When(fake => fake.Save()).Do(call => { throw new AggregateException(); });

// How do I make the second call to workItem.save() not throw any exception??

【问题讨论】:

    标签: c# unit-testing nsubstitute


    【解决方案1】:

    我刚刚发现解决方案超级简单

    workItem
              .When(x => x.Save())
              .Do(obj => { if (workItem.Field.Equals("Ok")) throw new AggregateException(new Exception("Assigned")); });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      相关资源
      最近更新 更多