在Rails控制器测试中,有没有办法模拟特定的远程IP?
我的应用程序中的某些功能的工作方式有所不同,具体取决于客户端的 IP 地址。有没有办法在 Rails 功能测试中测试这一点?我正在使用 Test::Unit 和 Shoulda。
Some functionality in my app works differently depending on the client's IP address. Is there a way to test that in Rails functional tests? I'm using Test::Unit and Shoulda.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过在控制器调用之前更改 REMOTE_ADDR 环境变量,您可以轻松地完成此操作,而无需存根。这是一个虚构的控制器,如果用户的 ip 地址是 1.2.3.4,则将用户重定向到主路径
: 以下是测试其工作原理的方法:
您在控制器调用之前设置远程 ip,因此您能够无需任何特殊插件或宝石即可伪造该数据。这是 ItemsController 的应该版本:
You can do this easily without stubbing, by changing the REMOTE_ADDR environment variable before your controller call. Here's a fictional controller that redirects the user to the home path if their ip address is 1.2.3.4:
Here's how you can test that it works:
You're setting the remote ip in advance of the controller call, so you're able to fake that data without any special plugins or gems. And here's the shoulda version, for ItemsController:
接收控制器需要一种检测 IP 地址(或将其作为参数接收)的方法,并且您需要一种为测试注入特定 IP 地址的方法,或者只是将测试 IP 地址指定为该方法的参数。
如果考虑到您的应用程序的功能,这并不容易,那么您可以做的就是将“自定义功能”应用于家庭网络上的另一台计算机(如果有的话),并以这种方式测试应用程序。但这并不能真正解决“通过单元测试解决它”的答案。
我可以问为什么你的应用程序会根据 IP 地址执行特定操作吗?也许还有另一种方式。
The receiving controller needs a method for detecting the IP address (or receiving it as a parameter), and you need a way to inject a specific IP address for your test, or just specifying the test IP address as a parameter to the method.
If that's not easy considering whatever your app does, what you could do is make the "custom functionality" apply to another computer on your home network, if you have one, and test the app that way. That doesn't really solve the "solve it through a unit test" answer though.
Can I ask why your app would perform a specific action depending on the IP address? Perhaps there's another way.
您应该能够通过删除请求并返回一个
request.remote_ip
等于所需 IP 地址的模拟对象来完成此操作。我会查看 FakeWeb 寻求帮助。它可以让您轻松设置预期的请求/响应。了解 Rails 本身的作用也可能会有所帮助这些类型的测试。
You should be able to accomplish this by stubbing out the requests and returning a mock object that has
request.remote_ip
equal to the desired IP address.I'd look at FakeWeb for help on this. It lets you easily set up the expected requests/responses. It might also be instructive to look at how Rails itself does these kinds of tests.
使用 gem“mocha”你可以这样做:
Using gem "mocha" you can do: