简体   繁体   中英

how to test a string with phpunit

I am creating this string

'Your maximum heart rate is ' . $int . ' Your body mass index is ' . $float . ' Your body fat percentage is ' . $float '%.'

and returning it

My assertion is

$stringFormat = 'Your maximum heart rate is %d. Your body mass index is %f.  Your body fat percentage is %f%.';

        $this->assertStringMatchesFormat($stringFormat, $actualResultm, 'message');

and when the test runs I get this error

FatControllerTest::testBodyInfo
message
Failed asserting that format description matches text.
--- Expected
+++ Actual
@@ @@
-Your maximum heart rate is %d. Your body mass index is %f.  Your body fat percentage is %f%.
+Your maximum heart rate is 193. Your body mass index is 43.181818181818. Your body fat percentage is 6.32302%.

Now I imagine it must have something to do with the fact that I am telling it to expect an unsigned int and two floats and it is returning one long string but if that is the case how do I test for a string like I have listed that is going to have 3 areas that change from case to case? If it is not that then what have I done wrong?

I think you got double space.

$stringFormat = 'Your maximum heart rate is %d. Your body mass index is %f.  Your body fat percentage is %f%.';
                                                                            ^ extra space

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM