Nsubstitute 根据对象属性返回不同

时间:2021-06-28 05:14:50

标签: c# unit-testing nsubstitute

我有如下代码。 第一次调用 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??

1 个答案:

答案 0 :(得分:0)

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

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