0

I have the following interface that I like to fake:

public interface ElementSettings
{
    ValueFormatter Formatter { get; }

    IEnumerable<ValidationRule> GetValidationRules();
}

I would like to throw an exception, when the Formatter is gotten. I tried it the following way:

var settings = Substitute.For<ElementSettings>();
var exception = new ArgumentException("alidsfjmlisa");
settings.When(s => { var tmp = s.Formatter; }).Throws(exception);

But I get allways a CouldNotSetReturnDueToNoLastCallException in the last line of the code. I have read all the hints in the exception message, but can't find any misusage.

scher
  • 1,813
  • 2
  • 18
  • 39

1 Answers1

1

Can you please post the exception output including stack trace? The following test passes for me:

    public class ValueFormatter { }
    public class ValidationRule { }

    public interface ElementSettings
    {
        ValueFormatter Formatter { get; }
        IEnumerable<ValidationRule> GetValidationRules();
    }

    [Test]
    public void Sample()
    {
        var sub = Substitute.For<ElementSettings>();
        var exception = new ArgumentException("alidsfjmlisa");
        sub.When(x => { var tmp = x.Formatter; }).Throw(exception);
        Assert.Throws<ArgumentException>(() =>
        {
            var tmp = sub.Formatter;
        });
    }
David Tchepak
  • 9,826
  • 2
  • 56
  • 68
  • 2
    Sometimes a small letter makes the difference. As I can see, i used the extension method `Throws` of the NUnit Framework instead of `Throw` (without `s`). That of course does not work. Thanks for your help. – scher Oct 16 '17 at 08:30