【问题标题】:Invalid setup on non-virtual member - on an interface?非虚拟成员上的无效设置 - 在接口上?
【发布时间】:2014-07-19 16:24:55
【问题描述】:

在使用 Moq 进行单元测试时,我收到以下错误:

Message: System.NotSupportedException : 
    Invalid setup on non-virtual (overridable in VB) member: 
    cm => cm.AppSettings[It.IsAny<string>()]

根据这些发现,我知道最好在 Moq 中使用抽象类或接口。

简而言之,我已经完成了我的作业。 =)

但是如果我真的在使用接口呢?

ConfigurationServiceTests

[TestFixture]
public class ConfigurationServiceTests {
    [Test]
    public void DialectShouldQueryConfigurationManagerAppSettings() {
        // Given
        configurationManagerMock
            .Setup(cm => cm.AppSettings[It.IsAny<string>()])
            .Returns(It.IsAny<string>());

        // When
        var dialect = configurationService.Dialect;

        // Then
        dialect.Should().BeOfType<string>();
        configurationManagerMock.Verify(cm => cm.AppSettings[It.IsAny<string>()]);
    }

    [SetUp]
    public void ConfigurationServiceSetUp() {
        configurationManagerMock = new Mock<IConfigurationManager>();
        configurationService = 
            new ConfigurationService(configurationManagerMock.Object);
    }

    private Mock<IConfigurationManager> configurationManagerMock;
    private IConfigurationService configurationService;
}

IConfigurationManager

public interface IConfigurationManager {
    NameValueCollection AppSettings { get; }
    ConnectionStringSettingsCollection ConnectionStrings { get; }
}

IConfigurationService

public interface IConfigurationService {
    string ConnectionDriver { get; }
    string ConnectiongString { get; }
    string Dialect { get; }
}

配置服务

public class ConfigurationService : IConfigurationService {
    public ConfigurationService(IConfigurationManager configurationManager) {
        this.configurationManager = configurationManager;
    }

    public string ConnectionDriver {
        get { return configurationManager.AppSettings["ConnectionDriver"]; }
    }

    public string ConnectionString {
        get { 
            return configurationManager
                .ConnectionStrings[ConnectionStringKey]
                .ConnectionString; 
        }
    }

    public string Dialect { 
        get { return configurationManager.AppSettings[DialectKey]; } 
    }

    private readonly IConfigurationManager configurationManager;

    private const string ConnectionStringKey = "DefaultConnectionString";
    private const string DialectKey = "Dialect";
}

我为什么要创建IConfigurationManager 接口?

除此之外,我想在我的生产代码中直接使用 Ninject 绑定它。所以我不需要接口的具体实现,因此我对上述异常感到非常惊讶。

kernel.Bind<IConfiguration>().To<ConfigurationManager>().InSingletonScope();

这样做可以让我对我的ConfigurationService 进行单元测试。

有什么想法吗?

【问题讨论】:

    标签: c# unit-testing ninject moq


    【解决方案1】:

    您正在尝试模拟 NameValueCollection 的索引器而不是您的属性 getter。

    您需要执行类似的操作(注意 SetupGet 而不是 Setup):

      .SetupGet(cm => cm.AppSettings).Returns(....)
    

    如果您想要“模拟”集合返回“任何东西”,您可能需要覆盖实际类(检查是否可以这样做)。

    如果您知道要查找的设置 - 从 Mock 返回填充的集合可能是一种选择:

     configurationManagerMock  
       .SetupGet(cm => cm.AppSettings)
       .Returns(new NameValueCollection { {"SettingName", "Value"}});
    

    另一种选择是返回一些自定义接口或IDictionary&lt;string,string&gt;,以便您也可以模拟索引:

    interface IConfigurationManager 
    {
       IDictionary<string,string> AppSettings { get; }
       ...
    

    【讨论】:

    • 是的,我知道要查询什么设置。完成建议的更改后,因为我使用的是It.IsAny&lt;string&gt;(),所以它总是返回 null,因此我的方言始终为 null,然后导致 dialect.Should()... 上的测试失败。你肯定帮了忙,我很喜欢! =)
    猜你喜欢
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 2012-09-27
    • 2014-03-13
    • 1970-01-01
    相关资源
    最近更新 更多