- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
Rails: RSpec - undefined method `cookie_jar' for nil:NilClass我的问题不同,因为我在 spec/requests
目录中有我的规范。我正在学习 Rails 教程
4) User Pages authorization as wrong user submitting a GET request to the Users#show action
Failure/Error: before { sign_in user, no_capybara: true }
NoMethodError:
undefined method `cookie_jar' for nil:NilClass
# ./spec/support/utilities.rb:6:in `sign_in'
# ./spec/requests/user_pages_spec.rb:77:in `block (4 levels) in <top (required)>'
spec/support/utilities.rb
include ApplicationHelper
def sign_in(user, options={})
if options[:no_capybara]
remember_token = User.new_remember_token
cookies[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
else
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
end
spec/requests/user_pages_spec.rb
....
context "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: 'wrong@example.com') }
before { sign_in user, no_capybara: true }
describe "submitting a GET request to the Users#edit action" do
before { get edit_user_path(wrong_user) }
specify { expect(response.body).not_to match('Edit user') }
specify { expect(response).to redirect_to(root_path) }
end
describe "submitting a PATCH request to the Users#update action" do
before { patch user_path(wrong_user) }
specify { expect(response).to redirect_to(root_url) }
end
describe "submitting a GET request to the Users#show action" do
before {get user_path(wrong_user) }
specify {expect(response.body).not_to match(user.name) }
specify { expect(response).to redirect_to(root_path) }
end
end
session Controller
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:email].downcase)
if user && user.authenticate(params[:password])
session[:user] = user.id
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
sign_out
redirect_to root_path
end
end
session 助手
module SessionsHelper
def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def sign_out
current_user.update_attribute(:remember_token, User.encrypt(User.new_remember_token))
cookies.delete(:remember_token)
self.current_user = nil
end
def current_user=(user)
@current_user = user
end
def current_user
remember_token = User.encrypt(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end
end
最佳答案
所以,发生这种情况的原因是 cookies
只是调用了 request.cookie_jar
。由此得出问题是 request
是 nil
要么在被测 Controller 上模拟 request
,要么使用将请求模拟到 Controller 中的东西。
关于ruby-on-rails - 未定义方法 `cookie_jar' 为 nil :NilClass for sign_in Rspec helper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21689988/
我有一个网站可以进行四种不同的重定向,而且似乎并不总是传递 cookie。 所以我在这里进行了研究并尝试了一些解决方案,例如: 创建一个临时 cookie_jar 并每次分配它。我也试过像这样手动传递
我正在根据用户的请求使用 Mechanize 抓取受密码保护的网站。我试图通过运行 Rake 任务来分离登录和搜索功能,该任务登录到站点并将 cookie 保存到数据库中,该数据库由后续的 Mecha
URL $url,重定向到https://auth.outside.com/secure/login用于通过 SSL 进行身份验证。一旦您登陆页面,该网站就会存储一些 cookie,并且还会在成功验证
如何打印正在设置的 cookie/cookie_jar 的值? 尝试: ##my $cookie_jar=HTTP::Cookies->new(file => "cookie.jar",autosav
Rails 新手。尝试遵循 Michael Hartl 的教程。 尝试添加辅助方法来模拟 RSpec 测试中的日志: describe "when the a user has logged in a
Rails: RSpec - undefined method `cookie_jar' for nil:NilClass我的问题不同,因为我在 spec/requests 目录中有我的规范。我正在学
我是一名优秀的程序员,十分优秀!