开发者

How to get a handle/reference to the current controller object inside a rails functional test?

I must be missing something very simple, but can't find the answer to this. I have a method named foo inside bar_controller. I simply want to call that method from inside a functional test.

Here's my controller:

class BarsController < ApplicationController
  def foo
    # does stuff
  end
end

Here's my functional test:

class BarsControllerTest << ActionController::TestCase
  def "test foo" do
    # run foo
    foo
    # assert stuff
  end
end

When I run the test I get:

NameError: undefined local variable or method `foo' for #<BarsControllerTest:0x102f2eab0>

All the documentation on functional tests describe how to simulate a http get request to the bar_controller which then runs the method. But I'd just like to run the method without hitting it with an h开发者_JS百科ttp get or post request. Is that possible?

There must be a reference to the controller object inside the functional test, but I'm still learning ruby and rails so need some help.


I found the answer in "Agile Web Development with Rails" Book. ActionController::TestCase initializes three instance variables needed by every functional test: @controller (contains instance of controller under test), @request, and @response.


You need call this action with HTTP verb like

get :foo
post :foo

etc...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜