Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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# 具有特定参数的最小空气质量配置_C#_Unit Testing_.net Core_Mstest_Autofixture - Fatal编程技术网

C# 具有特定参数的最小空气质量配置

C# 具有特定参数的最小空气质量配置,c#,unit-testing,.net-core,mstest,autofixture,C#,Unit Testing,.net Core,Mstest,Autofixture,今天我正在为我的一个类编写一个单元测试,它的构造函数中有一个参数。我试图冻结依赖项并创建sut configuration = builders.Freeze<IConfiguration>(); apiConfiguration = builders.Create<IAPIConfiguration>(); 它似乎没有以正确的方式嘲笑我,或者至少没有以我想要的方式嘲笑我。 我开始想知道有没有办法用可定制的键模拟IConfiguration类 更新: SUT: 到目

今天我正在为我的一个类编写一个单元测试,它的构造函数中有一个参数。我试图冻结依赖项并创建sut

 configuration = builders.Freeze<IConfiguration>();
 apiConfiguration = builders.Create<IAPIConfiguration>();
它似乎没有以正确的方式嘲笑我,或者至少没有以我想要的方式嘲笑我。 我开始想知道有没有办法用可定制的键模拟IConfiguration类

更新:

SUT:

到目前为止的测试用例:

[TestClass]
    public class UnitTest1
    {

        private readonly IFixture builders;
        private readonly string _apiKey;
        private readonly string _url;
        private readonly IAPIConfiguration apiConfiguration;
        private readonly IConfiguration configuration;

        public UnitTest1()
        {
            builders = new Fixture().Customize(new AutoMoqCustomization());
            _apiKey = builders.Create<string>();
            _url = builders.Create<string>();
            configuration = builders.Freeze<IConfiguration>();
            configuration["API:Key"] = "testKey";
            configuration["API:URL"] = "testUrl";

            apiConfiguration = builders.Build<IAPIConfiguration>().Create();
        }

        [TestMethod]
        public void TestMethod1()
        {
            Assert.AreSame(configuration["API:Key"], apiConfiguration.API_KEY);
        }
    }
[TestClass]
公共类UnitTest1
{
私有只读IFixture构建器;
私有只读字符串_apiKey;
私有只读字符串\u url;
私有只读IAPIConfiguration apiConfiguration;
专用只读IConfiguration配置;
公共单元测试1()
{
生成器=新夹具()。自定义(新的AutoMoqCustomization());
_apiKey=builders.Create();
_url=builders.Create();
configuration=builders.Freeze();
配置[“API:Key”]=“testKey”;
配置[“API:URL”]=“testUrl”;
apiConfiguration=builders.Build().Create();
}
[测试方法]
公共void TestMethod1()
{
Assert.arame(配置[“API:Key”],apiConfiguration.API\u Key);
}
}
在在线测试的建造商中测试制动器

apiConfiguration = builders.Build<IAPIConfiguration>().Create();
apiConfiguration=builders.Build().Create();

所做的就是创建一个模拟。它在配置模拟的行为时没有任何作用

[TestMethod]
public void TestMethod1() {
    //Arrange
    //Freeze-Build-Create sequence
    var fixture = new Fixture().Customize(new AutoMoqCustomization());
    var apiKey = fixture.Create<string>();
    var url = "http://example.com";
    var configuration = fixture.Freeze<IConfiguration>();

    //Configure the expected behavior of the mock
    var keys = new Dictionary<string, string> {
        { "API:Key" , apiKey },
        { "API:URL", url }
    };
    var mock = Mock.Get(configuration);
    mock.Setup(_ => _[It.IsAny<string>()]).Returns((string key) => keys[key]);

    IAPIConfiguration apiConfiguration = fixture.Build<APIConfiguration>().Create();

    //Act
    var actual = apiConfiguration.API_KEY;

    //Assert
    Assert.AreEqual(apiKey, actual);
    Assert.AreEqual(new Uri(url), apiConfiguration.URL);
}

修复与其原始设计相关的问题。

您是否已将模拟配置为在调用时按预期运行?@Nkosi,是的,我在任何其他代码生成器=new Fixture()之前有这一行。Customize(new AutoMoqCustomization());所做的只是创建一个模拟。它在配置mock的behavior@Nkosi,您的意思是类似这样的配置[“API:Key”]=“testKey”]?您能否提供一个配置mock行为的示例,因为我想,我不确定您在说什么?显示测试中的主题类和测试,因为它非常奇怪,但我仍然没有找到API密钥。它对你有用吗?我发布的代码是我用来测试的实际代码,它有效。这就是为什么我能够找到你的堆栈溢出错误和URL错误是。它起作用了。很抱歉给你带来了困惑。Visual Studio无法正常工作。现在一切都好了。谢谢
apiConfiguration = builders.Build<IAPIConfiguration>().Create();
[TestMethod]
public void TestMethod1() {
    //Arrange
    //Freeze-Build-Create sequence
    var fixture = new Fixture().Customize(new AutoMoqCustomization());
    var apiKey = fixture.Create<string>();
    var url = "http://example.com";
    var configuration = fixture.Freeze<IConfiguration>();

    //Configure the expected behavior of the mock
    var keys = new Dictionary<string, string> {
        { "API:Key" , apiKey },
        { "API:URL", url }
    };
    var mock = Mock.Get(configuration);
    mock.Setup(_ => _[It.IsAny<string>()]).Returns((string key) => keys[key]);

    IAPIConfiguration apiConfiguration = fixture.Build<APIConfiguration>().Create();

    //Act
    var actual = apiConfiguration.API_KEY;

    //Assert
    Assert.AreEqual(apiKey, actual);
    Assert.AreEqual(new Uri(url), apiConfiguration.URL);
}
public class APIConfiguration : IAPIConfiguration {
    public APIConfiguration(IConfiguration configuration) {
        this.API_KEY = configuration["API:Key"] ?? throw new NoNullAllowedException("API key wasn't found.");
        this._url = configuration["API:URL"] ?? throw new NoNullAllowedException("API url wasn't found.");
    }

    public string API_KEY { get; }

    private string _url;

    public Uri URL {
        get {
            return new Uri(this._url);
        }
    }
}