Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Stub/Mock在一个类中降低了2个级别_C#_Unit Testing_Mocking_Stub_Nsubstitute - Fatal编程技术网

C# Stub/Mock在一个类中降低了2个级别

C# Stub/Mock在一个类中降低了2个级别,c#,unit-testing,mocking,stub,nsubstitute,C#,Unit Testing,Mocking,Stub,Nsubstitute,我有一个不稳定的设置提供程序(公司遗留代码)。我试图将设置提供程序包装到设置存储库中,以减少不稳定代码的数量。因此,使用设置提供程序的方法不是20个,而是1个。其余部分实现SettingsRepository接口 不过,我在事后做测试时遇到了麻烦,这通常表明我做错了什么 我希望你能帮我找到什么 public class SettingsRepository : ISettingsRepository { public SettingsRepository() {Settings

我有一个不稳定的设置提供程序(公司遗留代码)。我试图将设置提供程序包装到设置存储库中,以减少不稳定代码的数量。因此,使用设置提供程序的方法不是20个,而是1个。其余部分实现SettingsRepository接口

不过,我在事后做测试时遇到了麻烦,这通常表明我做错了什么

我希望你能帮我找到什么

    public class SettingsRepository : ISettingsRepository
{
    public SettingsRepository() {Settings = Settings.Default; }
    public Settings Settings { get; set; }
}

public interface ISettingsRepository
{
    Settings Settings { get; set; }
}
我使用unity注入存储库。我从存储库中获取内容的方法如下:

_settingsRepository.Settings.SecureCache
这就是问题所在。我可以使用nsubstitute模拟/存根SettingsRepository接口,但我需要做的实际上是模拟“设置”以设置SecureCache的返回

有没有办法在nsubstitute中进行“深度模拟”,以便我可以执行以下操作:

_settingsRepository.Settings.SecureCache.Returns("www.somepath.com");
目前“设置”是空的,我没有任何可以在那里模拟的东西

我的备用解决方案是直接在SettingsRespository上添加所有设置字段,但我希望避免这样做,因为它只会将不稳定的代码移到解决方案中的其他位置。

使用NSubstitue(1.5.0.0版),您可以执行以下操作。您可以创建设置的实例(甚至可以创建一个假实例),然后返回假SecureCache,如下所示

 public class SettingsRepository : ISettingsRepository {
    public SettingsRepository() { Settings = Settings.Default; }
    public Settings Settings { get; set; }
}

public interface ISettingsRepository {
    Settings Settings { get; set; }
}

public class Settings {
    public Settings Default { get; set; }
    public string SecureCache { get; set; }
}

[TestFixture]
public class TestClass
{
    [Test]
    public void Subject_Scenario_Expectation()
    {
        var repoStub = Substitute.For<ISettingsRepository>();
        repoStub.Settings.Returns(new Settings() { SecureCache = "www.somepath.com" });

        Assert.IsNotNull(repoStub.Settings);
        Assert.IsNotNull(repoStub.Settings.SecureCache);
    }
}
公共类设置存储:ISettingsRepository{
公共设置存储(){Settings=Settings.Default;}
公共设置{get;set;}
}
公共接口正在设置存储{
设置设置{get;set;}
}
公共类设置{
公共设置默认值{get;set;}
公共字符串SecureCache{get;set;}
}
[测试夹具]
公共类TestClass
{
[测试]
公共无效主体\情景\预期()
{
var repoStub=替换为();
Returns(new Settings(){SecureCache=“www.somepath.com”});
Assert.IsNotNull(reposub.Settings);
Assert.IsNotNull(reposub.Settings.SecureCache);
}
}

听起来我的思路是对的。你能扩展“假实例”部分吗?设置是一个密封的类。感谢您的帮助很高兴您问:)假实例意味着真实实例的替代版本。例如,我可以从真实实例继承并创建自己的假版本。类FakeSettings:设置。然后您可以在单元测试中使用伪设置。因为它是密封的,所以你不能继承。因此,真正的实例将完成这项工作。这正是我在代码中看到的。