首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >RSpec:如何存根继承的方法current_user (没有设计)?

RSpec:如何存根继承的方法current_user (没有设计)?
EN

Stack Overflow用户
提问于 2014-07-02 11:13:19
回答 4查看 14.9K关注 0票数 27

我有一个基于MHartl的RoR4 Tutorial的控制器

就像MHartl一样,我没有使用Devise,而是I rolled my own authentication system

UsersController#Edit的RSpec有问题,因为视图调用了current_user.admin?,而控制器调用了@path_switch = path_switch

我不断收到类似以下内容的RSpec错误:

1) User Pages Edit 
 Failure/Error: visit edit_user_path(user)
 NoMethodError:
   undefined method `admin?' for nil:NilClass
 # ./app/controllers/users_controller.rb:106:in `path_switch'
 # ./app/controllers/users_controller.rb:53:in `edit'
 # ./spec/requests/user_pages_spec.rb:54:in `block (3 levels) in <top (required)>'

UsersController:

class UsersController < ApplicationController
  ...
  def edit
    @user = User.find(params[:id])
    @path_switch ||= path_switch                        #error
  end
  ...
  def path_switch
    if current_user.admin?                               #error
      users_path
    else
      root_path
    end
  end
end

我找到了这个really helpful article,它给了我希望,我在正确的轨道上,但我不能让它工作。

这是我所得到的(更新):

user_pages_spec.rb:

require 'spec_helper'
require 'support/utilities'

describe "User Pages" do
  #include SessionsHelper

  let(:user) { FactoryGirl.create(:user) }
  let(:current_user) {user}

  subject { page }

  describe "Edit" do
    before do
      sign_in(user)
      visit edit_user_path(user) 
    end

    it '(check links and content)' do
      should have_button('Submit')
      should have_link('Cancel')
      should have_content(user.fname+"\'s profile")
    end
   ...
  end
...
end

但是nilcurrent_user还是会回来的

如有任何帮助/指导,我们不胜感激。谢谢!

include SessionsHelper添加到我的user_pages_edit.rb的顶部describe块中似乎是在尝试使用来自该帮助器的sign_in(路径)。创建issue between RSpec and cookies.permanent。所以这就是突破口。

unfortunately,这让我回到了我的.admin?错误。

有两个对current_user.admin?的调用

One在控制器中:

  def path_switch
    if current_user.admin?    #error current_user == nil
      users_path
    else
      root_path
    end
  end

One在视图中显示为ERB:

<% if current_user.admin? %>
  <div class="row  col-xs-6 col-sm-6 col-md-3">
    <div class="input-group input-selector">
    ...

我所需要做的就是弄清楚如何设置current_user.admin = true并将其传递给控制器(希望还有视图),这样页面就可以加载了。为此,我所需要做的就是设置current_user = user,因为user.admin == true

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24522294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档