Rails/RSpec:'get'到另一个控制器

Pla*_*Ton 15 rspec ruby-on-rails-3

我在我的Rails 3 controllers/application.rb中有以下内容(我正在尝试编写规范):

  rescue_from CanCan::AccessDenied do |exception|
    flash[:alert] = t(:access_denied)
    if (current_user) then
      redirect_to root_url
    else
      session[:direct_redirect] = request.url
      redirect_to new_user_session_url
    end
  end
Run Code Online (Sandbox Code Playgroud)

我想写一些规格以确保功能正常.我已将测试放在spec/controller/application_controller_spec.rb中,并且我应该写一些如下的内容:

  it "should redirect to the root url" do
    controller.stub(:authorize!) { raise CanCan::AccessDenied }
    get :index
    response.should redirect_to(root_url)
  end
Run Code Online (Sandbox Code Playgroud)

问题是get:index line.规范抛出'无路由匹配控制器=>应用程序'错误."足够公平",我想,并尝试路由到不同的控制器.尝试是:

* get :controller => 'purchases', :action => :index
* get purchases_url
* get '/purchases/'
Run Code Online (Sandbox Code Playgroud)

所有这些都被解释为"应用程序"控制器下的一个动作.

那么,除了编写规范之外,我如何路由到rspec中的控制器?

Rya*_*igg 10

要测试那种你想要在RSpec中使用匿名控制器的东西.Relish在这里有一个很棒的例子.

请务必浏览该文档的其他部分.通过他们在那里的例子,我学到了很多东西.


小智 5

在你的规范中:

@controller = ADifferentController.new
expect(@controller).to receive(:this_other_thing)
get :index # or whatever
Run Code Online (Sandbox Code Playgroud)