-1

I'm trying to setup method OnChange in IOptionsMonitor from asp.net core. But it is not obvious for me, why Setup() doesn't work. Btw I saw similar examples here System.NotSupportedException: Unsupported expression: p => (p.UserProfileId == 1) And I have allmost the same. What is the reason of error?

Execution code:

var optionMonitorMock = new Mock<IOptionsMonitor<MyConfig>>();

optionMonitorMock
    .Setup(mock => mock.OnChange(It.IsAny<Action<MyConfig>>()));

Code falls with exception:

System.NotSupportedException. Unsupported expression: mock => mock.OnChange< MyConfig>(It.IsAny< Action< MyConfig>>()) Extension methods (here: OptionsMonitorExtensions.OnChange) may not be used in setup / verification expressions.

The asp.net's interface:

public interface IOptionsMonitor<out TOptions>
{
    TOptions CurrentValue { get; }
    TOptions Get(string name);
    IDisposable OnChange(Action<TOptions, string> listener);
}

The model:

public class MyConfig
{
    public int Id { get; set; }
    public string Options { get; set; }
}
  • 1
    I think the error is fairly self-explanatory? `OnChange(Action config)` is an extension method (which you haven't posted). The only `OnChange` method on `IOptionsMonitor` has the signature `OnChange(Action )`, which is different – canton7 Feb 19 '20 at 16:56

1 Answers1

0

The interface only exposes IDisposable OnChange(Action<TOptions, string> listener); . Your exception says that

Extension methods (here: OptionsMonitorExtensions.OnChange) may not be used in setup / verification expressions.

So you are going to have to use Setup with that method.

optionMonitorMock
    .Setup(mock => mock.OnChange(It.IsAny<Action<MyConfig,string>>()));
Felipe Faria
  • 569
  • 1
  • 5
  • 13