问题
I am testing my controller from Symfony2 with PHPUnit and the class WebTestCase
return self::$client->request(
'POST', '/withdraw',
array("amount" => 130),
array(),array());
$this->assertEquals(
"You can withdraw up to £100.00.",
$crawler->filter("#error-notification")->text());
But I get this error:
Expected: "You can withdraw up to £100.00."
Actual: "You can withdraw up to £100.00."
The thing is that in the webpage and the source code it looks fine, so I am thinking that maybe PHPUnit is having some trouble to fetch the text as UTF8?
What am I missing?
回答1:
solution:
Make sure the mbstring
extension is enabled.
There was a bug about failing tests with iconv
reported in the kohana bugtracker.
tips:
As proposed in this question/answer - you can test for correct UTF-8 output:
$this->assertEquals(
mb_detect_encoding(
crawler->filter("#error-notification")->text(),
'UTF-8'
),
'UTF-8'
);
You can include accept-charset
headers with requests sent by the client:
$client->request(
'POST', '/withdraw',
array("amount" => 130),
array(),
array(),
array('HTTP_ACCEPT_CHARSET' => 'utf-8')
);
来源:https://stackoverflow.com/questions/22017744/testing-special-characters-with-php-unit