开发者

Webrat looking for an action it does not need

开发者 https://www.devze.com 2023-03-21 23:35 出处:网络
Scenario I have a controller with two actions only - :create and :delete. Where the create action is defined thus:

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 Thi开发者_如何转开发ngsController

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消