Webrat 正在寻找它不需要的操作

发布于 2024-11-25 17:24:08 字数 930 浏览 0 评论 0原文

场景

我有一个控制器,只有两个操作 - :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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反差帅 2024-12-02 17:24:08

问题是: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文