PhpUnit和字符串(PhpUnit and string)
我开始学习PhpUnit和测试。 我有一个返回字符串的方法,我怎么能写一个测试来检查这个方法是否返回字符串。 这是我目前的代码:
方法:
/** * @return string */ public function living() { return 'Happy!'; }测试:
public $real; public $expected; public function testLiving() { $this->expected = 'Happy'; $this->real = 'Speechless'; $this->assertTrue($this->expected == $this->real); }I started learning PhpUnit and testing. I have a method which returns string, how I can write a test to check if this method returns string. Here is the code I have at this moment:
Method:
/** * @return string */ public function living() { return 'Happy!'; }Test:
public $real; public $expected; public function testLiving() { $this->expected = 'Happy'; $this->real = 'Speechless'; $this->assertTrue($this->expected == $this->real); }最满意答案
$this->assertTrue($this->expected == $this->real);是相同的
$this->assertEquals($this->expected, $this->real);请参阅https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertEquals
两者都检查给定的变量是否相等。
你可以检查变量是否是字符串
$this->assertTrue(is_string($this->real)); $this->assertTrue($this->expected == $this->real);is the same as
$this->assertEquals($this->expected, $this->real);See https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertEquals
Both check if given variables are equal.
You could check if variable is string
$this->assertTrue(is_string($this->real));更多推荐
发布评论