Chmelya
Chmelya

Reputation: 77

Moq System.NotSupportedException

I get it again and again

System.NotSupportedException: 'Unsupported expression: c => c.ProductMaxLenght
Non-overridable members (here: ConfigurationService.get_ProductMaxLenght) 
may not be used in setup / verification expressions.'

When I try call this

var configurationService = new Mock<ConfigurationService>();
configurationService.SetupGet(c => c.ProductMaxLenght).Returns(productMaxLength)

I call Initialize in Startup and take all data from Configuratiom Configuration Service:

public class ConfigurationService
    {
        private IConfiguration _configuration;
        private int? _productMaxLength;
        public int? ProductMaxLenght
        {
            get
            {
                if (!_productMaxLength.HasValue)
                {
                    throw new ArgumentNullException(nameof(_productMaxLength));
                }
                return _productMaxLength;
            }
        }

        public void Initialize(IConfiguration configuration)
        {
            _configuration = configuration ?? throw new ArgumentNullException(nameof(_configuration));
            _productMaxLength = _configuration.GetValue<int>("ProductsMaxLength");
        }
    }

Upvotes: 1

Views: 1330

Answers (1)

Julian
Julian

Reputation: 36720

You have to make the property overridable, so make it virtual:

public virtual int? ProductMaxLenght  { get ...

Upvotes: 2

Related Questions