使用设备身份验证进行 rspec 控制器测试
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否已通读 github 上的文档?:
Have you read through the docs on github?:
另一种选择
Another alternative