I'm using padrino and rspec and I'd like to be able to test a helper method that I wrote.
I have
spec/app/controllers/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
app/controllers/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
app/helpers/sessions_helper.rb
Testing.helpers do
def current_user
@current_user ||= User.find(:id=>session[:user_id]) if session[:user_id]
end
end
So this is really a two-part question. First part is that my method current_user is not even found. Second, I believe if it were found, it might throw errors as to session not being defined. But first things first, why am i getting 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)>'
This was answered in their issue tracker: https://github.com/padrino/padrino-framework/issues/930#issuecomment-8448579
Cut and paste from there:
Shorthand helpers (this is a Sinatra, not a Padrino feature, by the way) are hard to test for a reason:
is a shorthand for:
So the problem for testability is obvious: the helpers are an anonymous module and its hard to reference something that is anonymous. The solution is easy: make the module explicit:
and then test it in any fashion you want, for example:
Also, your example doesn't work because it assumes that helpers are global, which they are not: they are scoped by application.