如何在控制器规范中添加类似范围的方法

时间:2014-09-03 20:26:23

标签: ruby-on-rails ruby rspec dry rspec-rails

此问题的另一个变体:如何以DRY风格测试控制器范围?

我在控制器中有很多范围:

class SettingsController < ApplicationController
  before_filter :authenticate, :require_admin
  before_filter :navigation, only: [:show, :edit]

  def show
    flash[:notice] = I18n.t(:thank_you_for_your_subscription) if params[:p]
  end

  def edit
    render_not_found and return unless %w(username password).include?(params[:section])

    @section = params[:section]
  end
...

所以,我在控制器规范(动作)中有很多类似的方法。典型的控制器规格是:

describe SettingsController do
  let!(:user) { Factory(:user) }

  describe "GET #show" do
    def do_action
      get :show
    end

    it_requires_sign_in
    it_requires_admin
    it_specifies_navigation_with 'settings'

    it "should set a flash message when the 'p' param is set" do
      sign_in_as user
      do_action
      flash[:notice].should_not be_nil
    end
  end

  describe "GET #edit" do
    def do_action options = {}
      get :edit, options.merge(format: 'rjs')
    end

    it_requires_sign_in
    it_requires_admin
    it_specifies_navigation_with 'settings'

    context "when singed in" do
      before { sign_in_as user }

      it "should render 404 if no section is provided" do
        do_action
        response.code.should == '404'
      end
    end
  end
end

但是it_requires_sign_in方法有下一个表示:

def it_requires_sign_in
  it "should require sign in" do
    send do_action
    response.should redirect_to(sign_in_path)
  end
end

导航:

def it_specifies_navigation_with(name)
  it "specifies navigation with '#{name}'" do
    controller.should receive(:current_navigation).with(name)
    sign_in_as user
    do_action
  end
end

因此,它使用描述部分的方法。是否有可能制作某种范围,我可以指定方法(动作),我想检查或不检查,如:

describe SettingsController do
  requires_sign_in # every action (inner describe parts)
  requires_admin
  specifies_navigation_with only: [:show, :edit]

  let!(:user) { Factory(:user) }

  describe "GET #show" do
    def do_action
      get :show
    end

    it "should set a flash message when the 'p' param is set" do
      sign_in_as user
      do_action
      flash[:notice].should_not be_nil
    end
  end
...

主要问题是在精确范围内调用精确方法do_action(我的意思是描述行动部分)。

4 个答案:

答案 0 :(得分:3)

我不会集中精力去晒干。规格是一种文档。因此我认为规格应该是:

  • 可读:试图干得多可能导致规格不易阅读和理解。
  • 易于更改:您可能需要更改控制器中的操作。这应该是可能的,而无需更改其他操作的规范。

因此我认为可以在规格中重复一遍。

如果我必须指定控制器的那部分,我将从定义共享示例开始:

# shared example for authenticated admins 
RSpec.shared_examples 'an action only for authenticated admins' do
  describe 'before_filter :authenticate' do
    context 'with an user signed in' do
      before { sign_in_as(user) }

      it 'does not redirect' do
        expect(response).to_not redirect_to(sign_in_path)
      end

    end

    context 'without an user' do
      it 'redirects to the sign in page' do
        expect(response).to redirect_to(sign_in_path)
      end
    end
  end

  describe 'before_filter :require_admin' do
    before { sign_in_as(user) }

    context 'when user is an admin' do
      before { allow(user).to receive(:admin?).and_return(true) }

      it 'does something' do
        # check something
      end
    end

    context 'when user is not an admin' do
      before { allow(user).to receive(:admin?).and_return(false) }

      it 'does something other' do
        # check something other
      end
    end
  end

  describe 'before_filter :navigation' do
    before do 
      allow(controller).to receive(:current_navigation).and_return_original
      allow(user).to receive(:admin?).and_return(true) 
      sign_in_as(user)
    end

    it 'sets the current navigation name' do
      response
      expect(controller).to have_received(:current_navigation).with(navigation_name)
    end
  end
end

有关如何使用共享示例的更多示例,请参阅documentation

在控制器规范本身中,您会注意到我对准备我的示例和不同上下文的每一步都非常明确。此外,我更喜欢新expect(...).to语法而不是旧(...).should语法。

describe SettingsController do
  let(:user) { Factory(:user, :admin => true) }

  describe 'GET show' do
    subject(:response) { get :show }

    it_behaves_like 'an action only for authenticated admins' do
      let(:navigation_name) { 'settings' }
    end

    context 'with an admin signed in' do
      before { sign_in_as(user) }

      it 'shows a flash message' do
        response
        expect(flash[:notice]).to_not be_nil
      end
    end
  end

  describe 'GET edit' do
    subject(:response) { get :edit, { format: 'rjs' } }

    it_behaves_like 'an action only for authenticated admins' do
      let(:navigation_name) { 'settings' }
    end

    context 'with an admin signed in' do
      before { sign_in_as(user) }

      it 'shows a flash message' do
        expect(response.code).to eq('404')
      end
    end
  end
end

答案 1 :(得分:1)

&#39;与您分享我的想法,您可以决定它是否有用

describe SettingsController do
  def do_action_with(options={})
    get options[:action], options[:args]
  end

  shared_example_for 'a page that requires sign in' do
    do_action_with(action: action, args: args)
    response.should redirect_to(sign_in_path)
  end

  describe "GET #show" do
    it_behaves_like 'a page that requires sign in' do
      let(action) { :show }
      let(args) { [] }
    end

    it "should set a flash message when the 'p' param is set" do
      sign_in_as user
      do_action_with(action: :show, args: []) #maybe move these params as a global for the #show example
      flash[:notice].should_not be_nil
    end
  end

  describe "GET #edit" do
    it_behaves_like 'a page that requires sign in' do
      let(action) { :edit }
      let(args) { ['rjs'] }
    end
  end
end

答案 2 :(得分:1)

在大型项目中有很多RSpec经验我会建议另一种方法。虽然纯粹的BDD方法似乎是这样做的,但实际上它意味着由于没有开发人员想要运行的非关键测试而长时间运行的测试套件。

在您的给定示例中,测试每个控制器操作都需要管理员登录可能看起来是一种正确的方法,但您真正要做的是将控制器规格套件大小增加30%左右。问问自己,重构时开发人员只会删除before_filter :authenticate?你真正想要的是测试:authenticate方法本身。

那么它在实践中看起来如何?

describe ApplicationController do
  describe "#authenticate" do
    controller do
      before_action :authenticate

      def index
        render nothing: true
      end
    end

    it "redirects to sign in path if user is not signed in" do
      get :index
      response.should redirect_to(sign_in_path)
    end

    it "renders page if user is signed in" do
      sign_in @user
      get :index
      response.should be_success
    end
  end
end

请注意,我在这里测试应用程序控制器。我还测试身份验证方法本身,而不是测试是否调用了before_action :authenticate。注意:如果身份验证方法来自设计,我甚至不会编写这两个测试,只是信任设计作者测试过。

TL; DR - 测试方法的主体而不是调用它的单行宏。

答案 3 :(得分:0)

建立spickermann的答案,这就是我如何解决你的do_action问题。共享示例可以像下面那样传递参数:

RSpec.shared_examples "admin only route" do |method, route, params={}|
  it "fails without authentication" do
    send(method, route, params)
    expect(response.code).to eq("401")
  end

  it "succeeds with authentication" do
    login
    send(method, route, params)
    expect(response.code).not_to eq("401")
  end
  ...
end

为了在您的规范中使用它,请将其称为:

it_behaves_like("authenticated route", :post, :create, {name: 'bob'})