我正在使用ajax在我的页面中更新关注/取消关注用户。如果用户没有登录,并且他点击了下面的页面,那么他应该被重定向到登录页面:
def identify_user
@user = User.find_by(username: params[:id])
render js: "window.location = '/'" if @user.nil? || !user_signed_in?
end因为follow按钮设置了remote: true,所以我不能使用简单的redirect_to。如何使用水豚测试此功能?
scenario "Follow when user is not logged in", js: true do
sign_up_with("t@test.com","test1","secret12345")
click_link "logout"
visit "/test1" #takes to user 'test1' page
click_link "Follow test1" #link to follow user
expect(current_path).to eql('/')
end上面的代码不起作用,它只会打开我的默认浏览器。好的,随着window.location = '/'方法的改变来处理用户的重定向,这样测试更容易写也是受欢迎的!
发布于 2015-03-22 03:12:34
你能在页面上的一个元素上使用rspec element而不是current_path吗?事实证明,与简单地验证URL相比,这是一种更健壮的测试。类似于:
expect(page).to have_css('input#password')https://stackoverflow.com/questions/29173017
复制相似问题