我在使用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
我在 merchant model 中有一个已批准的范围。
scope :approved, where(:is_approved => true, :is_blacklisted => false)
现在我的问题是,尽管我存根了current_user并将@user作为current_user返回,但我的merchants_controller索引规范仍然失败。 但如果我注释掉authenticate_user!则规范通过,没有authenticate_user!会捕获index操作的调试器,但authenticate_user!不会捕获调试器。 我认为在subbing current_user中存在问题,但我无法弄清楚。 帮我解决这个问题..