2
我在使用单元测试框架phpspec2运行PUT
,POST
和DELETE
的测试时遇到了一些问题。PUT/POST/DELETE方法的测试
这是我在我的单元测试代码:
$this->beConstructedWith(new \Tonic\Application(), new \Tonic\Request(array(
'uri' => '/',
'method' => 'PUT',
'Content-Type' => 'application/json',
'data' => '{"name":"testing", "email":"[email protected]@test.com", "password":"Passw0rd"}'
)), array());
$expectedResult = new \stdClass();
$expectedResult->message = "An error was encountered.";
$expectedResult->error[] = "The email must be valid.";
$response = $this->exec();
$response->shouldReturnAnInstanceOf("Tonic\Response");
$response->contentType->shouldBe("application/json");
$response->body->shouldBe(json_encode($expectedResult));
环视Tonic代码,我在git repository的例子看我已经设置正确。
以下是这个单元测试运行bin/phpspec -v run
结果:
当我运行对进补服务的实际要求它工作正常。我知道我必须在单元测试的设置中做错了什么,但我不知道是什么。
编辑:put方法
/**
* Add a new user to the table.
*
* @method PUT
* @accepts application/json
* @provides application/json
* @json
* @return \Tonic\Response
*/
public function add()
{
// Validate the data before we go any futher.
$error = $this->validate();
// If the data is invalid then we want to let the requester know.
if (true === $error) {
$this->output->message = "An error was encountered.";
$this->responseCode = \Tonic\Response::NOTFOUND;
} else { // Else we want to PUT the data into our table.
$query = $this->db->prepare("INSERT INTO `user` (`name`, `email`, `password`, `dateOfBirth`) VALUES (:name, :email, :password, :dateOfBirth)");
$query->bindValue(":name", $this->request->data->name);
$query->bindValue(":email", $this->request->data->email);
$query->bindValue(":password", hash('sha256', $this->request->data->password));
$query->bindValue(":dateOfBirth", $this->request->data->dateOfBirth);
$query->execute();
// Check that the new user was successfully inserted into the database. If not let the requester know what happened.
if (0 === $query->rowCount()) {
if ("00000" === $query->errorCode()) {
$this->output->message = "No rows affected by query.";
} else {
$this->output->message = "There was an error running the query.";
$this->output->error[] = $query->errorInfo();
}
$this->responseCode = \Tonic\Response::CONFLICT;
} else { // Inserted successfully.
$this->output->message = "User successfully created.";
$this->responseCode = \Tonic\Response::CREATED;
$this->headers["Location"] = "/" . $this->request->data->email;
}
}
return new \Tonic\Response($this->responseCode, $this->output, $this->headers);
}
异常信息的哪一部分特别不明白吗? – hakre 2013-04-07 18:47:11
我知道这是说没有PUT的方法。但我不明白为什么它会这样说,因为有一种PUT的方法。我会将我的方法添加到问题中。 – Nalum 2013-04-07 18:57:21
可能不适用于URI'/'? – hakre 2013-04-07 19:00:01