c# – 非虚拟成员的无效设置 – 在接口上?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 非虚拟成员的无效设置 – 在接口上?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3221字,纯文字阅读大概需要5分钟。
内容图文
![c# – 非虚拟成员的无效设置 – 在接口上?](/upload/InfoBanner/zyjiaocheng/827/69b24739ed9c49229768f4dcb6c57391.jpg)
使用Moq进行单元测试时,出现以下错误:
Message: System.NotSupportedException :
Invalid setup on non-virtual (overridable in VB) member:
cm => cm.AppSettings[It.IsAny<string>()]
根据这些发现,我理解最好使用Moq的抽象类或接口.
> Why does the property I want to mock need to be virtual?
> Moq is throwing Invalid setup on a non-overridable member, when class is in Service Project
> Invalid setup on a non-virtual (overridable in VB) member
> Moq: Invalid setup on a non-overridable member: x => x.GetByTitle(“asdf”)
简而言之,我完成了我的作业. =)
但是,如果我实际使用的是接口呢?
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; }
}
ConfigurationService
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接口?
> Unit Testable Configuration Manager
除此之外,我想在我的生产代码中使用Ninject直接绑定它.所以我不需要具体的接口实现,因此我对上述异常感到非常惊讶.
kernel.Bind<IConfiguration>().To<ConfigurationManager>().InSingletonScope();
这样做可以让我对ConfigurationService进行单元测试.
任何的想法?
解决方法:
您正在尝试模拟NameValueCollection的索引器而不是属性getter.
您需要执行类似的操作(注意SetupGet而不是安装程序):
.SetupGet(cm => cm.AppSettings).Returns(....)
如果你想要“mocked”集合返回“任何东西”,你可能需要覆盖实际的类(检查是否有可能).
如果您知道要查找的设置 – 从Mock返回填充的集合可能是一个选项:
configurationManagerMock
.SetupGet(cm => cm.AppSettings)
.Returns(new NameValueCollection { {"SettingName", "Value"}});
另一种选择是返回一些自定义接口或IDictionary< string,string>所以你也可以模拟索引:
interface IConfigurationManager
{
IDictionary<string,string> AppSettings { get; }
...
内容总结
以上是互联网集市为您收集整理的c# – 非虚拟成员的无效设置 – 在接口上?全部内容,希望文章能够帮你解决c# – 非虚拟成员的无效设置 – 在接口上?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。