无法在我的帮助程序规范中存根current_or_guest_user

时间:2015-05-08 02:22:34

标签: ruby-on-rails rspec

我有一个帮助器,可以确保用户有权查看页面,并在用户没有权限时重定向:

module PermissionsHelper
  def require_permission(permission_attribute_name)
    return if current_or_guest_user.role.send(permission_attribute_name)
    redirect_to current_or_guest_user.role.landing_page,
                notice: 'You do not have sufficient permissions'
  end
end

方法current_or_guest_user是我在另一个帮助器中使用的方法,它返回当前用户,或者如果没有当前用户则创建并返回一个guest虚拟机。

我的规格如下:

require 'rails_helper'

RSpec.describe PermissionsHelper, type: :helper do
  describe 'requiring permissions' do
    let(:test_user) { create :customer }

    it "redirects the user to the user's landing page if the user doesn't have permission" do
      allow(helper).to receive(:current_or_guest_user) { test_user }
      require_permission(:view_admins)
      expect(response).to redirect_to test_user.landing_page
    end
  end
end

我收到了这个错误:

PermissionsHelper
  requiring permissions
    redirects the user to the user's langing page if the user doesn't have permission (FAILED - 1)

Failures:

  1) PermissionsHelper requiring permissions redirects the user to the user's langing page if the user doesn't have permission
     Failure/Error: require_permission(:view_admins)
     NameError:
       undefined local variable or method `current_or_guest_user' for #<RSpec::ExampleGroups::PermissionsHelper::RequiringPermissions:0x007fb7b430afe8>
     # /Users/user/.rvm/gems/ruby-2.2.2@project/gems/actionpack-4.2.1/lib/action_dispatch/testing/assertions/routing.rb:171:in `method_missing'
     # /Users/user/.rvm/gems/ruby-2.2.2@project/gems/actionview-4.2.1/lib/action_view/test_case.rb:271:in `method_missing'
     # ./app/helpers/permissions_helper.rb:3:in `require_permission'
     # ./spec/helpers/permissions_helper_spec.rb:11:in `block (3 levels) in <top (required)>'
     # /Users/user/.rvm/gems/ruby-2.2.2@project/gems/rspec-retry-0.4.0/lib/rspec/retry.rb:43:in `block (3 levels) in apply'
     # /Users/user/.rvm/gems/ruby-2.2.2@project/gems/rspec-retry-0.4.0/lib/rspec/retry.rb:34:in `times'
     # /Users/user/.rvm/gems/ruby-2.2.2@project/gems/rspec-retry-0.4.0/lib/rspec/retry.rb:34:in `block (2 levels) in apply'

Finished in 0.01188 seconds (files took 1.88 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/helpers/permissions_helper_spec.rb:9 # PermissionsHelper requiring permissions redirects the user to the user's langing page if the user doesn't have permission

编辑:我尝试将allow更改为allow_any_instance_of,现在我收到此错误:

使用此规范:

it "redirects the user to the user's langing page if the user doesn't have permission" do
  allow_any_instance_of(helper).to receive(:current_or_guest_user) { test_user }
  require_permission(:view_admins)
  expect(response).to redirect_to test_user.landing_page
end

我明白了:

Failure/Error: allow_any_instance_of(helper).to receive(:current_or_guest_user) { test_user }
     NoMethodError:
       undefined method `ancestors' for #<#<Class:0x007fe5d19a9a98>:0x007fe5d19a1668>

1 个答案:

答案 0 :(得分:0)

$idInscrito = (isset($_GET["id"])) ? $_GET["id"] : "1";

$nota_equipe = $_POST["nota_equipe"];
$nota_proposta_de_valor = $_POST["nota_proposta_de_valor"];
$nota_diferenciacao = $_POST["nota_diferenciacao"];
$nota_escalabilidade = $_POST["nota_escalabilidade"];
$nota_etapas_de_desenvolvimento = $_POST["nota_etapas_de_desenvolvimento"];
$nota_canais_de_comercializacao = $_POST["nota_canais_de_comercializacao"];
$nota_relevancia_mercado = $_POST["nota_relevancia_mercado"];
$nota_alinhamento_economia = $_POST["nota_alinhamento_economia"];
$nota_publico_alvo = $_POST["nota_publico_alvo"];
$nota_concorrentes = $_POST["nota_concorrentes"];
$nota_formas_de_consumo = $_POST["nota_formas_de_consumo"];
$nota_valores_de_investimentos = $_POST["nota_valores_de_investimentos"];
$nota_projecao_de_receitas = $_POST["nota_projecao_de_receitas"];

$sql = "UPDATE tb_inscricoes SET (data, nota_equipe, nota_proposta_de_valor, nota_diferenciacao, nota_escalabilidade, nota_etapas_de_desenvolvimento, nota_canais_de_comercializacao, nota_relevancia_mercado, nota_alinhamento_economia, nota_publico_alvo, nota_concorrentes, nota_formas_de_consumo, nota_valores_de_investimentos, nota_projecao_de_receitas) VALUES (now(), '$nota_equipe', '$nota_proposta_de_valor', '$nota_diferenciacao', '$nota_escalabilidade', '$nota_etapas_de_desenvolvimento', '$nota_canais_de_comercializacao', '$nota_relevancia_mercado', '$nota_alinhamento_economia', '$nota_publico_alvo', '$nota_concorrentes', '$nota_formas_de_consumo', '$nota_valores_de_investimentos', '$nota_projecao_de_receitas') WHERE id=$idInscrito";

$resultado = mysql_query($sql);

仅适用于静态方法。由于助手中的方法是实例方法,因此您需要使用

allow

http://rise4fun.com/Z3/qW3B

编辑:

以下代码段在我的一个项目中失败(使用'helper'变量)

allow_any_instance_of

但是,使用完整的类名称可以正常工作(至少为了测试方法模拟)

allow_any_instance_of(helper).to receive(:paginate).and_return('asdf')