在rspec测试中创建用户会话

时间:2017-11-13 10:44:10

标签: ruby session post rspec

我将在rspec测试中创建一个用户会话。但是我无法创建,因为它会出现以下错误:

stacktrace

我的测试代码:

describe Api::V1::CustomersController, type: :controller do
 describe '#create' do
  before(:each) do
    user = FactoryGirl.create(:superuser)
    UserSession.create(user)

    session[:user_id] = user.id
  end

  it 'test the create route' do
    json_data = '{"data":{"attributes":{"name":"theo test","website":"","iban":"","bic":"","bill_email":"",
              "patient_number_prefix":""},"type":""}}'

    post 'create', body: json_data, session: session, format: :json

    json = JSON.parse(response.body)

    expect(response).to have_http_status(:success)
    expect(json['data']).not_to be_nil
  end
 end
end

UserSession:

class UserSession < Authlogic::Session::Base
 self.last_request_at_threshold = 10.minutes

 # Convenience method for the persistence_token
 public def persistence_token
   record&.persistence_token
 end
end

CustomerController:

class Api::V1::CustomersController < ApplicationController
  public def create
    action(Customer::Create)
    .otherwise('customer_create_error', 401)
    .then_render(Customer::Representer::Out::Default)
  end
end

我的会话位于config/initializers/session_store.rb定义:

Rails.application.config.session_store :cookie_store, key: '_example_session'

operations.rb

module Webapp::ControllerHelper::Operations
 public def action(op = nil, effective_params = params)
  return if op.nil?

  # Call the operation
  result = op.(effective_params,
   'current_user' => current_user,
   'document' => request.raw_post)

  # Build and return the OperationResult object
  Webapp::OperationResult.new(result, method(:render))
 end
end

有人知道为什么吗?

0 个答案:

没有答案
相关问题