OpenRasta 单元测试
我即将开始 OpenRasta 项目(一个基于 http Web 服务的 xml)的工作。 OpenRasta 看起来很棒,但不幸的是,互联网上的有效示例似乎很少。查看项目的测试部分,如果我的处理程序返回强类型对象(不是 OperationResult),即:
public class PersonHandler
...
public Person Get(int id)
{
...
如何测试 http 状态代码? (例如,如果处理程序抛出未捕获的异常)。我不确定测试的级别是什么,以及需要模拟什么(顺便说一句,使用起订量)
任何帮助,特别是编码示例!
I’m about to start work on an OpenRasta project (an xml over http web service). OpenRasta looks great but unfortunately worked examples seem few and far between on the internet. Looking at the test side of the project, if my handlers are returning strongly typed objects (not OperationResult), i.e.:
public class PersonHandler
...
public Person Get(int id)
{
...
How can I test for http status codes? (For example if the handler throws an uncaught exception). I’m not sure what level the tests pitch in at, and what needs mocking (using moq btw)
Any help appreciated, particularly coded examples!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,最终将我的测试编写为更高级别的集成测试,实际上通过一个简单的 HttpWebRequest 客户端进行真正的 REST/HTTP 调用。这使我能够检查 HTTP 响应标头/状态代码,并从客户端的角度仔细检查 JSON/XML 序列化,这与操作是否成功同样重要。
我首先从所有处理程序返回 OperationResult,并使用它们来包装强类型对象。我的处理程序都继承自带有一些辅助方法的基类,这些方法可以更轻松地返回带有用户友好的错误消息的自定义错误。我编写的代码越多,我的处理程序就越类似于 ASP.NET MVC 控制器。例如:
然后在测试客户端中,只需检查 HTTP 状态代码就非常容易了。显然这对嘲笑没有多大帮助。我不确定最好的解决方案是什么,事实上,我很喜欢这个问题,希望有人能比我更好地回答它 - 但到目前为止,这对我来说效果很好。
I faced the same problem, and ended up writing my tests as integration tests at a much higher level, actually making real REST/HTTP calls through a simple HttpWebRequest client. This allowed me to check the HTTP response headers / status codes and double-check the JSON/XML serialization from the client's perspective, which was just as important as whether or not the operations succeeded.
I started by returning OperationResult from all my handlers, and used these to wrap the strongly-typed objects. My handlers all inherit from a base class with a few helper methods that make it easier to return a custom error with a user-friendly error message. The more I coded this up, the more my handlers resembled a ASP.NET MVC controller. e.g.:
Then in the test client, it's pretty easy to just check the HTTP status code. Obviously this doesn't help much with mocking. I'm not sure what the best solution is, in fact I've favorited this question in the hope that someone answers it better than I can - but this has worked pretty well for me so far.
处理程序只是一个类(理想情况下具有最小的依赖性),因此您的单元测试可以只测试类中的隔离逻辑。
如果您想测试状态代码,我建议(基于很少的经验!)使用 OpenRasta 自托管。
这是我最近编写的一个测试(有所更改):
我应该补充一下,主机是在 TestInitialize 方法中设置的,如下所示:
...并在 TestCleanup 方法中清理。
The handler is just a class--ideally with minimal dependencies--so your unit tests can just test the isolated logic in the class.
If you want to test for status codes, I recommend (based on very little experience!) using OpenRasta self-hosting.
Here's a test (somewhat changed) that I wrote recently:
I should add that the host is set up in the TestInitialize method as such:
...and is cleaned up in the TestCleanup method.