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/7/sql-server/26.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
使用matlab测试框架对异常执行单元测试时出错_Matlab_Unit Testing_Exception - Fatal编程技术网

使用matlab测试框架对异常执行单元测试时出错

使用matlab测试框架对异常执行单元测试时出错,matlab,unit-testing,exception,Matlab,Unit Testing,Exception,我在文件夹+WTrade/+Database/@PostgreSQLConnectionOptions中有以下类: classdef PostgreSQLConnectionOptions

我在文件夹
+WTrade/+Database/@PostgreSQLConnectionOptions
中有以下类:

classdef PostgreSQLConnectionOptions
我想对这个类执行单元测试。我参加了下面的考试

classdef postgresqlconnectionoptiontest
当我尝试使用以下脚本运行测试时:

导入matlab.unittest.TestSuite
导入matlab.unittest.constraints.Throws;
databaseSuite=TestSuite.fromFolder(“tests/WTrade/Database”);
结果=运行(数据库套件);
我得到以下错误:

>> runtests('PostgreSQLConnectionOptionsTest','ProcedureName','testWrongHostnameArray')
Running PostgreSQLConnectionOptionsTest
32        options = WTrade.Database.PostgreSQLConnectionOptions();

================================================================================
Error occurred in PostgreSQLConnectionOptionsTest/testWrongHostnameArray and it did not run to completion.
    ---------
    Error ID:
    ---------
    'MATLAB:TooManyOutputs'
    --------------
    Error Details:
    --------------
    Error using WTrade.Database.PostgreSQLConnectionOptions/setHostname
    Too many output arguments.

    Error in PostgreSQLConnectionOptionsTest/testWrongHostnameArray (line 34)
          this.verifyError(options.setHostname(hostName), 'WTrade:invalidParameter');
================================================================================
.
Done PostgreSQLConnectionOptionsTest
__________

Failure Summary:

     Name                                                    Failed  Incomplete  Reason(s)
    =======================================================================================
     PostgreSQLConnectionOptionsTest/testWrongHostnameArray    X         X       Errored.

ans = 

  TestResult with properties:

          Name: 'PostgreSQLConnectionOptionsTest/testWrongHostnameArray'
        Passed: 0
        Failed: 1
    Incomplete: 1
      Duration: 2.0606
       Details: [1×1 struct]

Totals:
   0 Passed, 1 Failed (rerun), 1 Incomplete.
   2.0606 seconds testing time.
我不理解
'MATLAB:TooManyOutputs'
错误。我已经在matlab中直接尝试了该方法,它的工作原理与预期相符;我测试参数是否为字符串,如果不是,则启动异常

当我运行以下命令时:

a=WTrade.Database.PostgreSQLConnectionOptions
a、 setHostname([“p1”、“p2”)
我得到了预期的错误

Error using WTrade.Database.PostgreSQLConnectionOptions/setHostname (line 43)
WTrade.Database.PostgreSQLConnectionOption.setHostname: name array size must be 1

那么,为什么我不能在测试用例中正确捕获异常?我如何才能正确检查我是否抛出异常,以便通过测试?

您需要将函数句柄传递给verifyError。现在发生的是您正在调用
this.verifyError(options.setHostname(hostName),'WTrade:invalidParameter');
实际调用的是
选项。setHostname(hostName)
,其中一个输出参数作为参数传递给verifyError。它应该是:

this.verifyError(@()选项.setHostname(主机名),'WTrade:invalidParameter');

另外,我建议使用标准属性
Hostname
,而不是编写setter方法,使用验证语法,如下所示:

classdef PostgreSQLConnectionOptions < handle

  properties 
    Hostname(1,1) string = "";
  end

end
但请注意,在使用属性时,为了使用verifyError,需要将其包装到函数中:

classdef PostgreSQLConnectionOptionsTest < matlab.unittest.TestCase

  methods (Test)

    function testWrongHostnameArray(this)
      options = WTrade.Database.PostgreSQLConnectionOptions();
      hostName = ["h1" "h2"];
      this.verifyError(@setHostname, 'WTrade:invalidParameter');
      function setHostname
          options.Hostname = hostname;
      end
    end

  end
end
classdef postgresqlconnectionoptiontest

尽管您需要将其包装到函数中进行测试,但大体上使用属性是一种更好的体验。我们有,而且我认为最好利用它。

谢谢。我会尽快检查解决方案。对于封装:代码与此不同,我调整它只是为了显示问题。
classdef PostgreSQLConnectionOptionsTest < matlab.unittest.TestCase

  methods (Test)

    function testWrongHostnameArray(this)
      options = WTrade.Database.PostgreSQLConnectionOptions();
      hostName = ["h1" "h2"];
      this.verifyError(@setHostname, 'WTrade:invalidParameter');
      function setHostname
          options.Hostname = hostname;
      end
    end

  end
end