如何在rails中使用stub

时间:2013-10-23 14:15:47

标签: ruby-on-rails ruby-on-rails-3 rspec rspec-rails

我有以下代码片段

def index
    session[:previous_url] = request.referer 

   if session[:previous_url].blank?
     redirect_to registration_path(id: current_user.associate_username)
   end 
end

我正在为它写一个测试 测试:

describe 'index' do
  it 'renders the index page' do
    controller.stub(current_user: FactoryGirl.create(:admin_user))
    session = {}
    session[:previous_url] = 'http://example.com/new'
    get :index, params, session
    expect(response).to render_template(:index)
  end
end

这不允许我通过blank?。有没有人知道我可以通过blank?返回false的任何其他方式,因此不会重定向。感谢

2 个答案:

答案 0 :(得分:0)

您可以在request.env["HTTP_REFERER"]块中指定before。像这样:

  before do
    request.env["HTTP_REFERER"] = "http://localhost:3000/"
  end

答案 1 :(得分:0)

怎么样?

describe 'index' do
  it 'renders the index page' do
    controller.stub(current_user: FactoryGirl.create(:admin_user),
                    session: {previous_url: "http://example.com/new"})
    get :index, params, session
    expect(response).to render_template(:index)
  end
end