Testing helpers in Rails 3 with Rspec 2 and Devise

时间秒杀一切 提交于 2019-12-04 16:42:08

问题


My helper code looks like this (and works fine btw):

module ProvidersHelper
  def call_to_review(provider)
    if user_signed_in? && review = Review.find_by_provider_id_and_user_id(provider.id, current_user.id)
      link_to "Edit Your Review", edit_provider_review_path(provider, review), :class => "call_to_review"
    else
      link_to "Review This Provider", new_provider_review_path(provider), :class => "call_to_review"
    end
  end
end

Unfortunately, this produces the following error when I run my tests:

 undefined method `user_signed_in?' for #<ActionView::Base:0x00000106314640>
 # ./app/helpers/providers_helper.rb:3:in `call_to_review'

Clearly the Devise::Controllers::Helpers are not being included in my helpers when rspec is running the test. Any suggestions that might help this work?

Edit: to provide a bit more information, my spec_helper does have this:

config.include Devise::TestHelpers, :type => :controller
config.include Devise::TestHelpers, :type => :view
config.include Devise::TestHelpers, :type => :helper

(Sadly, I couldn't get it to work with :type => [:controller, :view, :helper])

Anyway I believe that these lines add the sign_in(scope, object) (and other) test helpers to your tests. They don't add the helpers that you would actually leverage in your controller / view code.


回答1:


I think the philosophy of rspec is to test the view/helpers/models in total isolation as much as possible. So in this case, i would stub out the user_signed_in? and returns false or true and my results should change appropriately. This gives you a clean isolated test.




回答2:


Are you currently including the test Helpers as suggested in the wiki?

# spec_helper.rb:
RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

type would be probably helper in your case.




回答3:


Maybe try putting this is in a before block?

  @request.env["devise.mapping"] = :user



回答4:


This has not been solved to my satisfaction and probably never will be. I think the best work-around for now is to manually stub helper.current_user and any other Devise methods you use in the helper method you're testing.

Yes, Devise provides these stubbing facilities for controller and view specs. I suspect that it's something about the combination of Devise/Rails/Test::Unit/Rspec that proves this to be difficult for helper specs.




回答5:


my helper test uses Devise and cancan and works without stubbing anything (but I'm not sure if it is better to actually stub everything).

Here's the gist: https://gist.github.com/shotty01/5317463 i also tried to add user_signed_in? in the helper method and it still was fine.

The following is required:

add to spec_helper.rb:
config.include Devise::TestHelpers, :type => :helper

My spec gems:

rspec (2.10.0)
rspec-core (2.10.1)
rspec-expectations (2.10.0)
rspec-mocks (2.10.1)
rspec-rails (2.10.1)

of course you can sign in without factory girl, you just have to rewrite the ValidUserHelper methods to create a user directly or from fixtures.



来源:https://stackoverflow.com/questions/4663897/testing-helpers-in-rails-3-with-rspec-2-and-devise

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