Rspec test to check user gets a self-populated ip_location attribute filled (Rails 4/devise/geocoder) -
In my sign-up page, I have implemented an IP tracking feature, when a user signs up, the country It comes from the user, so to fill the 'user_country' feature of the newly created account
This works but as a newbie I do not know how to test with RSPC To do that, it works. Sign up to create a user, then get the account After a Brahmin, that user_country is no longer empty.
How can I use the /app/controllers/registrations_controller.rb method to assign a country here. User's newly created record on controllers / registration_controller - see below
class registration controller & lt; Wise: RegistrationsController Layout 'Lightbox' def updated account_update_params = devise_parameter_sanitizer.sanitize (: account_update) [: password]. Required for blank settings # When the password if the account_update_params are left blank form to submit? Account_update_params.delete ("password") account_update_params.delete ("password_confirmation") end @user = User.find (current_user.id) if @ user.update (account_update_params) # rails 4 .update form set_flash_message .update_attributes with effect only Done: notice: update # sign-in @ user, password: bypass = & gt; Real redirect_to after_update_path_for (@user) second to "edit" end-end # # rails for 4 strong parameters def resource_params params.require (user) .permit (: email,: password, password_confirmation, current_password, user_country) Private end: Resource_params protected def after_sign_up_path_for (resource) resource.update (user_country: set_location_by_ip_lookup.country) #Usage concerns / ApplicationController root_path end is loaded by CountryCetter
If it helps , So in this way I am looking for the country of visitor:
module country Master ActiveSupport expand :: first before worrying: set_location_by_ip_lookup end # We use geocoder gem # output: ip address def set_location_by_ip_lookup if Rails.env.development? Or Rails.env.test? Geocoder.search (request.remote_ip). First, the second request.location end end
the wanted. Describe the UserController 'The place with the GeoCoder determines the location'. What geocoder = class_double ("GeoCoder") # square double place = double ('location', country: 'some country') # Returns expected return value with basic values Is (geocoder). To receive (: search) .and_return (location) # Badass Return Price Post: Create, user_params user = Assign (user) is expected (user user_country). AEC ('some countries') and end
In this way, we make a fake location and geocoder, and also make fake return value. So we believe that the return value from geocoder remains as a user attribute. In this way, we do not really need to check whether the GeoCoder
is working, and simplifies the installation of our tests.
The concept used here:
If you want to be stronger, then you may be able to find fake IP in the context of the test request. Then, argue what the fake geodor is coming with logical matching:
In general, you should use the schema to isolate the concerns of your tests. For more documentation see
Comments
Post a Comment