Testing helpers in Rails 3 with Rspec 2 and Devise

非 Y 不嫁゛ 提交于 2019-12-03 10:44:50

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.

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.

Maybe try putting this is in a before block?

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

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.

Shotty

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.

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