我在我的集成测试中使用我的会话存储return_to
URL时遇到问题。无法在导轨集成测试中使用会话变量
因为我的控制器可以从不同的地方访问我引荐存储在新行动的会议,并重定向到它在我创建行动。
cards_controller.rb:
class CardsController < ApplicationController
...
def new
@card = current_user.cards.build
session[:return_to] ||= request.referer
end
def create
@card = current_user.cards.build(card_params)
if @card.save
flash[:success] = 'Card created!'
redirect_to session.delete(:return_to) || root_path
else
render 'new', layout: 'card_new'
end
end
...
end
正如我只用在我的测试中创造的行动,我想设置会话变量在集成测试,因为我在我的单元测试做的,但它不工作。我总是被重定向到根路径。
cards_interface_test.rb:
class CardsInterfaceTest < ActionDispatch::IntegrationTest
test 'cards interface should redirect after successful save' do
log_in_as(@user)
get cards_path
assert_select 'a[aria-label=?]', 'new'
name = "heroblade"
session[:return_to] = cards_url
assert_difference 'Card.count', 1 do
post cards_path, card: { name: name, icon: 'white-book', color: 'indigo', contents: 'subtitle | Rogue feature'}
end
assert_redirected_to cards_url
follow_redirect!
assert_match name, response.body
assert_select 'td', text: name
end
end
assert_redirected_to
行上的测试失败。
我试着打电话get new_card_path
第一次,但没有什么区别,现在我有点失落。我不知道这是否应该基本上工作,但我犯了一个小错误,或者如果我试图完全对付最佳做法,并应重构所有我的界面测试使用像Selenium或类似的工具。
我试着以及提供会话变量像铁轨引导请求的一部分描述为没有效果功能测试:
post cards_path, {card: { name: name, icon: 'white-book', color: 'indigo', contents: 'subtitle | Rogue feature' }}, {'return_to' => cards_url}
工作,thx。没有想到我必须明确地设置引用者。 –