Webrat 正在寻找它不需要的操作
场景
我有一个控制器,只有两个操作 - :create
和 :delete
。创建操作的定义如下:
def create
# some code...
if @thing.save
redirect_to :back, :notice => "Successfully created."
else
redirect_to :back, :notice => "Successfully deleted."
end
end
使用...链接到该操作
<%= link_to "Become a friend", things_path(...), :method => :post %>
我在视图中
。问题
当我在浏览器中与应用程序交互时,这工作正常。但是,我希望使用 Webrat 的辅助方法 - click_link“成为朋友”
使用 RSpec 集成测试来测试此功能 - 我认为这是正确的。但我收到此错误
Failure/Error: click_link "I like Person-1's taste"
AbstractController::ActionNotFound:
The action 'index' could not be found for ThingsController
我可以在事物控制器中创建一个空索引操作,但这会违反 KISS 原则< /a>.
问题
我该如何解决/解决这个问题?对于这样的情况有什么最佳实践吗?
Scenario
I have a controller with two actions only - :create
and :delete
. Where the create action is defined thus:
def create
# some code...
if @thing.save
redirect_to :back, :notice => "Successfully created."
else
redirect_to :back, :notice => "Successfully deleted."
end
end
I link to the action using...
<%= link_to "Become a friend", things_path(...), :method => :post %>
...in the view.
Problem
This works fine as I interact with the app in my browser. However, I wish to test this functionality using RSpec integration testing using Webrat's helper method - click_link "Become a friend"
- which I think is correct. But I get this error
Failure/Error: click_link "I like Person-1's taste"
AbstractController::ActionNotFound:
The action 'index' could not be found for ThingsController
I can create an empty index action in the Things controller but this would violate the KISS Principle.
Questions
How can I workaround/fix this? And are there any best practices for cases like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是:method => :post 使 Rails 创建一个表单,然后在您单击链接时提交该表单。这仅适用于启用了 javascript 的情况,而 webrat 不支持开箱即用。查看 https://github.com/brynary/webrat/wiki 并尝试获取使用硒运行测试。由于 selenium 实际上使用真正的浏览器,因此您的规范应该可以运行。
The problem is that :method => :post makes rails create a form which is then submitted when you click the link. This only works with javascript enabled which webrat does not support out of the box. Have a look at https://github.com/brynary/webrat/wiki and try to get the test running with selenium. As selenium actually uses a real browser, your specs should run.