简体   繁体   中英

CouldNotSetReturnDueToNoLastCallException when throwing exception

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM