使用Rspec测试我的操作时,redirect_to无效

时间:2015-01-05 20:01:31

标签: ruby-on-rails ruby rspec

我正在测试一个动作,但我无法通过。

我的规格:

it 'redirects to the datatables page for authenticated users' do
    user = User.create(name: 'fer', email: 'fer@fer.fer', password: 'fer')
    session[:user_id] = user.id
    get :new
    expect(response).to redirect_to datatables_lists_path
  end

我的代码:

 def create
    user = User.where(email: params[:email]).first
    if user && user.authenticate(params[:password] )
      session[:user_id] = user.id
      redirect_to datatables_lists_path, notice: 'you are in!! :)'

    else
      flash[:error] = 'it's not working'
      redirect_to sign_in_path
    end
  end

我得到的信息是:

  1) SessionsController redirects to the datatables page for authenticated users
     Failure/Error: expect(response).to redirect_to datatables_lists_path
       Expected response to be a <redirect>, but was <200>

1 个答案:

答案 0 :(得分:0)

您正在发布get :new,但是您向我们展示了您的控制器的create操作。您的测试不是测试创建操作。