2017-05-24 108 views
1

我想在rspec控制器測試中設置Rails.cache,但緩存在控制器方法內始終爲空。這是做什麼的方法?如何在rspec控制器測試中設置Rails緩存

用戶模型

def user_token 
Rails.cache.fetch(id) 
end 

用戶控制器

if current_user.user_token 
     @user = @account.users.find(params[:id]) 
     @user.revoke_seat(:admin, current_user) 
     render :template => "/admin/users/revoke_seat" 
    else 
     render :js => "window.location.href='#{server_url}/oauth/authorize?response_type=code&client_id=#{client_id}&state=#{request.referrer}?auto_revoke_seat=true&redirect_uri=#{auth_service_callback_url}"; 
    end 

Rspec的

before do 
    users(:admin).stub(:internal_admin?).and_return(true) 
    login_as :admin 
    Rails.cache.write(user.id, "testToken", expires_in: 2.minutes) 
    end 

    it "should redirect to authentication service to generate access token" do 
     expect(user).to receive(:user_token).and_return(true) 
     xhr :put, :revoke_seat, account_id: account.id, id: user.id 
     expect(response).to render_template('admin/users/revoke_seat') 
     expect(assigns(:account)).to eq(account) 
     expect(assigns(:user)).to eq(user) 
    end 

回答

0

我會改變之前的方法爲:

before do 
    users(:admin).stub(:internal_admin?).and_return(true) 
    login_as :admin 
    allow(Rails.cache).to receive(:fetch).with(user.id).and_return("testToken") 
end 
相關問題