gpt4 book ai didi

ruby-on-rails - Rspec/Capybara 模拟 session ( stub 或模拟?)

转载 作者:行者123 更新时间:2023-12-04 18:06:45 29 4
gpt4 key购买 nike

我正在尝试访问'/requests/new。现在在 Controller 代码中,我已经编写了它,以便如果 session 不存在,用户将被重定向回主页。像这样:

def new
@pagetitle = "What would you like to borrow?"

if session[:signup_email].nil?
flash[:danger] = "Please enter your email to get started"
redirect_to root_path
else
@signup_parent = Signup.find_by_email(session[:signup_email].downcase)
if @signup_parent.tos != true || @signup_parent.streetone.blank? || @signup_parent.streettwo.blank? || @signup_parent.zipcode.blank?
flash[:danger] = "Almost there! We just need a little more info"
redirect_to edit_signup_path
else
@requestrecord = @signup_parent.requests.build
end
end

结束

但问题是现在在测试中,我无法模拟 session 。我试着写:

    before do
session[:signup_email] = Signup.find_by_id(@signup_ana.id)
visit '/requests/new'
save_and_open_page
fill_in '#borrow__1', :with => 1
click_button
end

但这引发了一个错误:undefined local variable or methodsession' for #`

然而,如果我不尝试模拟它,save_and_open_page 会告诉我,我只是被重新路由回了家。我应该怎么做?有没有办法将其 stub /模拟出来??

最佳答案

你可以试试这个 gem rack_session_access

然后就可以在 capybara 中调用page.set_rack_session(:user_id => user.id)

关于ruby-on-rails - Rspec/Capybara 模拟 session ( stub 或模拟?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24857140/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com