0

我有一个继承自ApplicationController. 为了测试之前的过滤器Admin::BaseController,我在这个规范中创建了一个匿名控制器。

require 'spec_helper'

describe Admin::BaseController do

  it { should be_a(ApplicationController) }

  controller do
    def index
      render :text => ''
    end
  end

  context 'when current user is not an admin' do
    it 'redirects to root path' do
      get :index
      response.should redirect_to(root_path)
    end
  end
end

但是当我提出index行动请求时,它不会调用 before 过滤器Admin::BaseController

当我定义该过滤器ApplicationController而不是Admin::BaseController运行测试时,它可以工作。显然这个匿名控制器继承自ApplicationController. 我怎样才能改变这种行为?

4

1 回答 1

0

我在这里找到了答案;https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/anonymous-controller

controller(Admin::BaseController) do
  def index
    render :text => ''
  end
end

成功了。

于 2013-04-13T19:33:25.227 回答