how to use padrino helper methods in rspec

时光怂恿深爱的人放手 提交于 2019-12-06 09:35:04

问题


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)>'

回答1:


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:

MyApp.helpers do
  def foo

  end
end

is a shorthand for:

helpers = Module.new do
  def foo
  end
end

MyApp.helpers helpers

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:

module MyHelpers
  def foo
  end
end

MyApp.helpers MyHelpers

and then test it in any fashion you want, for example:

describe MyHelpers do
  subject do
    Class.new { include MyHelpers } 
  end

  it "should foo" do
    subject.new.foo.should == nil
  end
end

Also, your example doesn't work because it assumes that helpers are global, which they are not: they are scoped by application.



来源:https://stackoverflow.com/questions/11302394/how-to-use-padrino-helper-methods-in-rspec

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!