Guest User

Untitled

a guest
Aug 27th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. Cucumber BDD with capybara and devise integration
  2. Feature: Sign in
  3. Scenario: User signs in successfully with email
  4. Given I am a new, authenticated user
  5. When I go to the tour page
  6. Then I should be signed in
  7.  
  8. Given /^I have ones+user "([^"]*)" with password "([^"]*)"$/ do |email, password|
  9. u = User.new(:email => email,
  10. :password => password,
  11. :password_confirmation => password)
  12. u.skip_confirmation!
  13. u.save!
  14. end
  15.  
  16. Given /^I am a new, authenticated user$/ do
  17. email = 'user@test.com'
  18. password = 'please'
  19.  
  20. Given %{I have one user "#{email}" with password "#{password}"}
  21. And %{I go to the sign in page}
  22. And %{I fill in "user_email" with "#{email}"}
  23. And %{I fill in "user_password" with "#{password}"}
  24. And %{I press "Log Me In"}
  25. end
  26.  
  27. Then /^I should be signed in$/ do
  28. And %{I should see "Sign out"}
  29. end
  30.  
  31. require "spec_helper"
  32.  
  33. # In order to fake a Devise sign-in user in integration testing ; just do it :
  34. # DeviseSessionMock.enable(Factory :user)
  35. #
  36. # BEWARE: Sometimes this mock screw up your integration testing suite. (because stubbed methods are not reloaded suite after suite)
  37. # So, you HAVE TO call DeviseSessionMock.disable. The easiest way to ensure everything will be ok is to setup a before.each in RSpec.configure :
  38. # RSpec.configure do |config|
  39. # config.before :each, :type => :integration do
  40. # DeviseSessionMock.disable
  41. # end
  42. # end
  43. #
  44. # BEWARE: Don't call this mock for functionnal (controller_test)... Devise provide support for this kind of testing
  45.  
  46. module DeviseSessionMock
  47.  
  48. # configuration options, store controllers & helpers to stubs.
  49. CONTROLLERS_TO_STUB = [FooController]
  50. HELPERS_TO_STUB = [FooHelper]
  51.  
  52. mattr_accessor :current_user
  53.  
  54. # public interfaces
  55. # fakes devise current_user
  56. def self.enable(mock_user)
  57. setup(mock_user)
  58. end
  59.  
  60. # frees devise current_user
  61. def self.disable()
  62. setup()
  63. end
  64.  
  65. private
  66. # helpers
  67. # setup session mocking.
  68. # if user is nil, you are not signed in
  69. # if user is not nil, you are signed in
  70. def self.setup(mock_user = nil)
  71. @@current_user = mock_user
  72. enable_for_controller
  73. enable_for_helper
  74. end
  75.  
  76. def self.signed_in?
  77. DeviseSessionMock.current_user.nil? ? false : true
  78. end
  79.  
  80. # Fake logged in user for controller
  81. def self.enable_for_controller
  82. CONTROLLERS_TO_STUB.each do | class_name |
  83. class_name.any_instance.stubs(:authenticate_user!).returns(signed_in?)
  84. class_name.any_instance.stubs(:current_user).returns(DeviseSessionMock.current_user)
  85. class_name.any_instance.stubs(:user_signed_in?).returns(signed_in?)
  86. end
  87. end
  88.  
  89. # Fake logged in user for helper
  90. # we use define_method cause stubbing on module does not works (or i failed...)
  91. def self.enable_for_helper
  92. HELPERS_TO_STUB.each do | module_name |
  93. module_name.send(:define_method, :user_signed_in?, Proc.new{signed_in?})
  94. module_name.send(:define_method, :current_user, Proc.new{DeviseSessionMock.current_user})
  95. end
  96. end
  97. end
Add Comment
Please, Sign In to add comment