我正在使用 padrino 和 rspec,我希望能够测试我编写的辅助方法。
我有
规范/应用程序/控制器/sessions_controller_spec.rb
describe "POST /sessions" do
it "should populate current_user after posting correct user/pass" do
u = User.create({:email=>"john@gmail.com", :password=>"helloworld", :password_confirmation=>"helloworld"})
user = {
email:"john@gmail.com",
password:"hellowolrd"
}
post '/sessions/new', user
current_user.should_not == "null"
end
end
应用程序/控制器/sessions_controller.rb
post "/new" do
user = User.authenticate(params[:email], params[:password])
if user
session[:user_id] = user.id
redirect '/'
else
render "sessions/new"
end
end
应用程序/helpers/sessions_helper.rb
Testing.helpers do
def current_user
@current_user ||= User.find(:id=>session[:user_id]) if session[:user_id]
end
end
所以这真的是一个两部分的问题。第一部分是我的方法 current_user 甚至没有找到。其次,我相信如果找到它,它可能会抛出关于未定义会话的错误。但首先,为什么我得到 undefined_method current_user?
Failures:
1) SessionsController POST /users should populate current_user after posting correct user/pass
Failure/Error: current_user.should_not == "null"
NameError:
undefined local variable or method `current_user' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2:0x0000000313c0d8>
# ./spec/app/controllers/sessions_controller_spec.rb:20:in `block (3 levels) in <top (required)>'