Skip to content

Commit fec21c3

Browse files
committed
Per #16, test exception messages
1 parent bf7b985 commit fec21c3

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

src/Codeception/Specify.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,56 @@ private function getSpecifyExamples($params)
8585

8686
private function getSpecifyExpectedException($params)
8787
{
88-
$throws = false;
8988
if (isset($params['throws'])) {
90-
$throws = $params['throws'];
89+
$throws = (is_array($params['throws'])) ? $params['throws'][0] : $params['throws'];
90+
9191
if (is_object($throws)) {
9292
$throws = get_class($throws);
9393
}
9494
if ($throws === 'fail') {
9595
$throws = 'PHPUnit_Framework_AssertionFailedError';
9696
}
97+
98+
$message = (is_array($params['throws']) && isset($params['throws'][1])) ? $params['throws'][1] : false;
99+
100+
return [$throws, $message];
97101
}
98-
return $throws;
102+
103+
return false;
99104
}
100105

101106
private function specifyExecute($test, $throws = false, $examples = array())
102107
{
108+
$message = false;
109+
110+
if (is_array($throws)) {
111+
$message = $throws[1];
112+
$throws = $throws[0];
113+
}
114+
103115
$result = $this->getTestResultObject();
104116
try {
105117
call_user_func_array($test, $examples);
106118
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
107-
if ($throws !== get_class($e)) $result->addFailure(clone($this), $e, $result->time());
119+
if ($throws !== get_class($e)){
120+
$result->addFailure(clone($this), $e, $result->time());
121+
}
122+
123+
if ($message !==false && $message !== $e->getMessage()) {
124+
$f = new \PHPUnit_Framework_AssertionFailedError("exception message '$message' was expected, but '" . $e->getMessage() . "' was received");
125+
$result->addFailure(clone($this), $f, $result->time());
126+
}
108127
} catch (\Exception $e) {
109128
if ($throws) {
110129
if ($throws !== get_class($e)) {
111130
$f = new \PHPUnit_Framework_AssertionFailedError("exception '$throws' was expected, but " . get_class($e) . ' was thrown');
112131
$result->addFailure(clone($this), $f, $result->time());
113132
}
133+
134+
if ($message !==false && $message !== $e->getMessage()) {
135+
$f = new \PHPUnit_Framework_AssertionFailedError("exception message '$message' was expected, but '" . $e->getMessage() . "' was received");
136+
$result->addFailure(clone($this), $f, $result->time());
137+
}
114138
} else {
115139
throw $e;
116140
}

tests/SpecifyTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function testSpecification()
1616
$this->user->name = 'jon';
1717
$this->assertEquals('jon', $this->user->name);
1818
});
19-
19+
2020
$this->assertEquals('davert', $this->user->name);
2121

2222
$this->specify('i can fail here but test goes on', function() {
@@ -57,7 +57,7 @@ function testAfterCallback()
5757
$this->user = "jon";
5858
});
5959
$this->assertEquals('davert', $this->user);
60-
}
60+
}
6161

6262
function testMultiAfterCallback()
6363
{
@@ -104,6 +104,29 @@ public function testExceptions()
104104
}, ['throws' => 'fail']);
105105
}
106106

107+
public function testExceptionsWithMessages()
108+
{
109+
$this->specify('user is invalid', function() {
110+
throw new Exception("test message");
111+
}, ['throws' => ['Exception', 'test message']]);
112+
113+
$this->specify('user is invalid', function() {
114+
throw new RuntimeException("test message");
115+
}, ['throws' => ['RuntimeException', 'test message']]);
116+
117+
$this->specify('user is invalid', function() {
118+
throw new RuntimeException("test message");
119+
}, ['throws' => [new RuntimeException(), "test message"]]);
120+
121+
$this->specify('i can handle fails', function() {
122+
$this->fail("test message");
123+
}, ['throws' => ['fail', 'test message']]);
124+
125+
$this->specify('ignores an empty message', function() {
126+
$this->fail("test message");
127+
}, ['throws' => ['fail']]);
128+
}
129+
107130
/**
108131
* @expectedException RuntimeException
109132
*/
@@ -140,7 +163,7 @@ public function testDeepCopy()
140163
$this->assertEquals(2, $this->a->prop->prop);
141164
});
142165
$this->assertEquals(1, $this->a->prop->prop);
143-
166+
144167
}
145168

146169
public function testConfiguration()

0 commit comments

Comments
 (0)