当尝试编写一些控制器规范来测试我的路由设置时,在尝试传递会话信息且会话哈希为空(session == {}
)时会遇到问题。
routes.rb
的相关部分具有我想要测试的以下约束:
Rails.application.routes.draw do
...
get 'places/review_index', constraints: MapOwnershipRestriction.new, to: 'review#review_index', as: :review_index
...
end
MapOwnershipRestriction
看起来像这样
class MapOwnershipRestriction
def matches?(request)
token = request[:map_token]
map = Map.find_by(secret_token: token)
user = User.find_by(id: request.session[:user_id])
map.user == user ? true : false
end
end
现在,由于request.session[:user_id]
为空,因以下的会话信息导致以下(示例)测试失败:
it 'populates the right place to be reviewed' do
~ @user = create :user
~ @map = create :map, :full_public, user: @user
~ @new_place = create :place, :unreviewed, name: 'SomeName', map: @map
~ session[:user_id] = @user.id
+ expect(get: review_place_path(id: @new_place.id, map_token: @map.secret_token)).to be_routable
end
到目前为止,所有文章和SO Q& As都没有帮助我。
最好和thx提前, 岸堤