-1

I have codes like below. The first call of save(), I want it to throw an exception. And the second call when obj.field="ok", I do not want it to throw exception. How do i achieve this through 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??
Yu Lan
  • 1
  • 2

1 Answers1

0

I just found the solution super easy

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