我在lib/user_session.rb中有這樣的代碼在rspec上測試ruby中的會話裝飾器lib
其中一個控制器通過包含UserSession來使用它。
我該如何測試它?我已經在spec/lib/user_session.rb中添加了測試,但代碼依賴於我不確定如何模擬的會話。
module UserSession
def viewed
session[:viewed] ||= Array.new
return session[:viewed]
end
def viewed_add(id, name)
if viewed.select{|b| b[:id] == id}.empty?
session[:viewed] << {:id => id, :name => name}
end
return session[:viewed]
end
def viewed_delete(id)
unless id.nil?
session[:viewed].delete_if{|b| b[:id] == id}
return session[:viewed]
end
end
end