PhpUnit和字符串(PhpUnit and string)

编程入门 行业动态 更新时间:2024-10-18 23:24:35
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));

更多推荐

本文发布于:2023-07-18 04:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154863.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   PhpUnit   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!