使用phpunit测试异常消息

时间:2014-12-18 17:05:18

标签: php phpunit

我的phpunit测试:

<?php
class TestTest extends PHPUnit_Framework_TestCase
{
        /*
         * @expectedExceptionMessage success
         */
    public function testExceptionMessage() {
        throw new Exception('success');
    }
}

单元测试失败。这是phpunit的输出:

There was 1 error:

1) TestTest::testExceptionMessage
Exception: success

/path/to/TestTest.php:8

FAILURES!                          
Tests: 1, Assertions: 0, Errors: 1.

在我看来,测试应该是成功的,因为Exception消息是成功的,这是@expectedExceptionMessage所期望的?

2 个答案:

答案 0 :(得分:3)

添加@expectedException注释,它应该可以正常工作

/**
 * @expectedException Exception
 * @expectedExceptionMessage success
 */
 public function testExceptionMessage() {
    throw new Exception('success');
 }

答案 1 :(得分:1)

在抛出PHPUnit之前,您需要发出警报:

  1. 期待例外
  2. 期待您所抛出的特定消息。
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = @"Title";
content.body = @"Body";
content.sound = [UNNotificationSound defaultSound];
NSURL *imageURL = [NSURL fileURLWithPath:@"example.png"];
NSError *error;
UNNotificationAttachment *icon = [UNNotificationAttachment attachmentWithIdentifier:@"image" URL:imageURL options:nil error:&error];
if (error)
{
    NSLog(@"error while storing image attachment in notification: %@", error);
}
if (icon)
{
    content.attachments = @[icon];
}