使用设备身份验证进行 rspec 控制器测试

发布于 2024-12-05 02:27:05 字数 1462 浏览 0 评论 0原文

我在 rspec 测试控制器和设备身份验证方面遇到问题。

我有以下设置,

已包含

config.include Devise::TestHelpers, :type => :controller

在我的spec_helper.rb中

在我的merchants_controller_spec.rb中

describe MerchantsController do
  before :each do
    @user = Factory(:user)
    @merchant = Factory(:merchant, :user_id => @user.id,:is_approved => false, :is_blacklisted => false)
    controller.stub!(:current_user).and_return(@user)
  end
  describe "GET index" do
    it "assigns all merchants as @merchants" do
      merchant = Factory(:merchant,:is_approved => true, :is_blacklisted => false)
      get :index
      assigns(:merchants).should eq([merchant])
    end
  end
end

我的merchants_controller.rb

class MerchantsController < ApplicationController

  before_filter :authenticate_user!
  def index
    @merchants = Merchant.approved
    debugger
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @merchants }
    end
  end
end

我有一个在商人模型中批准的范围

scope :approved, where(:is_approved => true, :is_blacklisted => false)

现在我的问题是,即使我存根current_user并返回@user作为current_user,我的merchants_controller索引规范正在失败。但如果我注释掉authenticate_user!那么规范就通过了,

没有authenticate_user!索引操作的调试器被捕获,但使用authenticate_user!调试器没有被捕获。

我认为 subbing current_user 存在问题,我无法弄清楚。

帮帮我吧..

I am having problem with rspec testing controller the devise authentication.

I have a following setup

I have included

config.include Devise::TestHelpers, :type => :controller

in my spec_helper.rb

In my merchants_controller_spec.rb

describe MerchantsController do
  before :each do
    @user = Factory(:user)
    @merchant = Factory(:merchant, :user_id => @user.id,:is_approved => false, :is_blacklisted => false)
    controller.stub!(:current_user).and_return(@user)
  end
  describe "GET index" do
    it "assigns all merchants as @merchants" do
      merchant = Factory(:merchant,:is_approved => true, :is_blacklisted => false)
      get :index
      assigns(:merchants).should eq([merchant])
    end
  end
end

My merchants_controller.rb

class MerchantsController < ApplicationController

  before_filter :authenticate_user!
  def index
    @merchants = Merchant.approved
    debugger
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @merchants }
    end
  end
end

I have a scope approved in merchant model

scope :approved, where(:is_approved => true, :is_blacklisted => false)

Now my problem is even though i stubbed current_user and returned @user as current_user, My merchants_controller index spec is failing. But if i comment out authenticate_user! then the spec passes,

without authenticate_user! the debugger of index action is caught but with authenticate_user! debugger is not caught.

I think there is problem in subbing current_user and i am not able to figure it out.

Help me out..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻安 2024-12-12 02:27:05

您是否已通读 github 上的文档?:

Devise 包含一些功能规格的测试助手。要使用它们,您只需在测试类中包含 Devise::TestHelpers 并使用 sign_insign_out 方法。这些方法与控制器中的方法具有相同的签名:

sign_in :user, @user   # sign_in(scope, resource)
sign_in @user          # sign_in(resource)

sign_out :user         # sign_out(scope)
sign_out @user         # sign_out(resource)

Have you read through the docs on github?:

Devise includes some tests helpers for functional specs. To use them, you just need to include Devise::TestHelpers in your test class and use the sign_in and sign_out methods. Such methods have the same signature as in controllers:

sign_in :user, @user   # sign_in(scope, resource)
sign_in @user          # sign_in(resource)

sign_out :user         # sign_out(scope)
sign_out @user         # sign_out(resource)
神爱温柔 2024-12-12 02:27:05

另一种选择

RSpec.describe YourController, :type => :controller do
  before do
    user = FactoryGirl.create(:user)
    allow(controller).to receive(:authenticate_user!).and_return(true)
    allow(controller).to receive(:current_user).and_return(user)
  end

  # rest of the code
end

Another alternative

RSpec.describe YourController, :type => :controller do
  before do
    user = FactoryGirl.create(:user)
    allow(controller).to receive(:authenticate_user!).and_return(true)
    allow(controller).to receive(:current_user).and_return(user)
  end

  # rest of the code
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文