2015-06-06 66 views
0

應用程序/控制器/ new_controller.rbrspec的控制器的空會話

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 

規格/控制器/ new_controller_spec.rb

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")expect(assigns(session[:dr])).to eq("dr")作品

回答

1

請嘗試:expect(session['dr']).to eq("dr")

session值與assigns分開。