rspec控制器空会话

时间:2015-06-06 16:30:30

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

应用/控制器/ new_controller.rb

events = Events.objects.filter(Q(date__day=day,is_weekly=False) | Q(date__day=day%7,is_weekly=True))

spec / controllers / new_controller_spec.rb

  class NewController < ApplicationController
   def enter_time
    session[:dr] = 'dr'
    if !params[:time].nil? then
      Time.zone = @franchise.time_zone
      inanhour = 1.hour.since(Time.zone.now)

      delivery_time = delivery_time_format(params[:time])
      delivery_time = delivery_time.change(offset: (Time.zone.now.utc_offset / 3600).to_s)
      session[:target_time] = [delivery_time, inanhour].max.to_s
    end
    end
   end

抛出错误:

require 'spec_helper'

describe NewController, :type => :controller do
  before do
    allow(controller).to receive(:current_user) { nil }
  end

  describe "POST /enter_time" do
    let(:addr_str) { '2810 Derby St, Berkeley, CA, United States' }
    let(:delivery_time) { '06/06/2015 12:15 PM' }

    it "sets current time of time zone" do
      address = VCR.use_cassette('retrieve_address #{addr_str}', record: :new_episodes) do
        Address.retrieve_address(addr_str)
      end

      a = { id: address.id, text: addr_str, lat: address.lat, lng: address.lng, src: :text }

      post :enter_time, { time: delivery_time }
      expect(assigns(session['dr'])).to eq("dr")
    end
  end
end

expected: "dr" got: nil expect(assigns(session['dr'])).to eq("dr")都不起作用

1 个答案:

答案 0 :(得分:1)

尝试:expect(session['dr']).to eq("dr")

session值与assigns分开。