最小起订量 System.NotSupportedException

Moq System.NotSupportedException

我一次又一次地得到它

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

当我尝试调用这个时

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

我在 Startup 中调用 Initialize 并从 Configuration 中获取所有数据 配置服务:

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

您必须使 属性 可覆盖,因此使其成为 virtual:

public virtual int? ProductMaxLenght  { get ...