RSpec:如何存根继承的方法 current_user(无设计)? [英] RSpec: how to stub inherited method current_user (w/o Devise)?

查看:44
本文介绍了RSpec:如何存根继承的方法 current_user(无设计)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 MHartl 的RoR4 教程

I have a controller based on MHartl's RoR4 Tutorial

和 MHartl 一样,我没有使用 Devise,我推出了我自己的身份验证系统

And just like MHartl, I'm not using Devise, I rolled my own authentication system

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

Having trouble with the RSpec for UsersController#Edit since the view has a call to current_user.admin? and the controller calls @path_switch = path_switch

我不断收到以下方面的 RSpec 错误:

I keep getting RSpec errors along the lines of:

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)>'

用户控制器:

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

<小时>

我发现这篇非常有用的文章让我希望我在正确的轨道上,但我无法让它工作.


I found this really helpful article that gives me hope that I'm on the right track, but I can't get it to work.

这是我得到的(更新):

Here's as far as I've gotten (updated):

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

但是 current_user 还是回来了 nil

感谢任何帮助/指导.谢谢!

Any help/guidance is appreciated. Thanks!

include SessionsHelper 添加到我的 user_pages_edit.rb 的顶部描述块似乎尝试使用该助手的 sign_in(path).在 RSpec 和 cookies.permanent 之间创建 问题代码>.所以这是一个半身像.

Adding include SessionsHelper to the top describe block of my user_pages_edit.rb seems to try and use the sign_in(path) from that helper. Creating an issue between RSpec and cookies.permanent. So that's a bust.

不幸的是,这让我又回到了我的 .admin? 错误.

unfortunately, this brings me right back to my .admin? error.

有两次调用 current_user.admin?

一个在控制器中:

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

一个是作为 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.

All I need to do is figure out how to set current_user.admin = true and pass it to the controller (and then hopefully the view) so that the page can load. To do that, all I need to do is set current_user = user because user.admin == true.

推荐答案

也可以使用

user = create(:user) #FactoryBot

allow(controller).to receive(:current_user).and_return(user)

这篇关于RSpec:如何存根继承的方法 current_user(无设计)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆