📜  PHPUnit assertIsNotScalar()函数

📅  最后修改于: 2022-05-13 01:56:31.069000             🧑  作者: Mango

PHPUnit assertIsNotScalar()函数

assertIsNotScalar()函数是 PHPUnit 中的内置函数,用于断言标量变量是包含整数、浮点数、字符串或布尔值的变量。此函数不认为 null 是标量。如果实际值为标量,则此断言将返回 true,否则返回 false。如果为真,则断言的测试用例通过,否则测试用例失败。

句法:

assertIsNotScalar($actual[, $message = ''])

参数:该函数接受上面提到的两个参数,如下所述:

  • $actual:这个参数是代表实际数据的任何类型的字符串或变量。
  • $message:此参数采用字符串值。当测试用例失败时,此字符串消息显示为错误消息。

下面的例子说明了 PHPUnit 中的 assertIsNotScalar()函数:

示例 1:

PHP
assertIsNotScalar(
          
            $actualvalue, 
            "actual value is a scalar or not"
        );
          
    }
  
  
     public function testNegativeForassertIsNotScalar()
    {   
     $actualvalue = 420;
            // Assert function to test whether actual 
        // value is a integer or not
        $this->assertIsNotScalar(
          
            $actualvalue, 
            "actual value is a scalar or not"
        );
          
    }
   
 } 
?>


PHP
assertIsNotScalar(
          
            $actualvalue, 
            "actual value is a scalar or not"
        );
  
    }
      
}
  
?>


输出:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

FF                                                 2 / 2 (100%)

Time: 90 ms, Memory: 10.00 MB

There were 2 failures:

1) GeeksPhpunitTestCase::testPositiveForassertIsNotScalar
actual value is a scalar or not
Failed asserting that 'Null' is not of type "scalar".

/home/lovely/Documents/php/test.php:15

2) GeeksPhpunitTestCase::testNegativeForassertIsNotScalar
actual value is a scalar or not
Failed asserting that 420 is not of type "scalar".

/home/lovely/Documents/php/test.php:29

FAILURES!
Tests: 2, Assertions: 2, Failures: 2.

方案二:

PHP

assertIsNotScalar(
          
            $actualvalue, 
            "actual value is a scalar or not"
        );
  
    }
      
}
  
?>

输出:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

.                                                1 / 1 (100%)

Time: 90 ms, Memory: 10.00 MB

OK (1 test, 1 assertion)