I wanted to add some custom fields to the user registration so I overrode the devise registration controller. Now I'm trying to write a test that should (I would think) create a new, unregistered user. The test fails though and I'm not sure what to troubleshoot from here.
# note: this is a dump of `:valid_attributes` by my test
{"id"=>nil, "name"=>"Hans Swift", "email"=>"Hans.Swift_397@testing.com", "encrypted_password"=>"$2a$04$T6mRo3Dzcv6iV7Kdh52E6OA0tAX7nE4y3skV2jzkD9CLr5v8Dri1K", "reset_password_token"=>nil, "reset_password_sent_at"=>nil, "remember_created_at"=>nil, "sign_in_count"=>0, "current_sign_in_at"=>nil, "last_sign_in_at"=>nil, "current_sign_in_ip"=>nil, "last_sign_in_ip"=>nil, "confirmation_token"=>nil, "confirmed_at"=>nil, "confirmation_sent_at"=>nil, "unconfirmed_email"=>nil, "failed_attempts"=>0, "unlock_token"=>nil, "locked_at"=>nil, "created_at"=>nil, "updated_at"=>nil}
F
Failures:
1) Users::RegistrationsController POST #create with valid params creates a new User
Failure/Error:
expect {
puts valid_attributes
post :create, {:user => valid_attributes}, valid_session
}.to change(User, :count).by(1)
expected #count to have changed by 1, but was changed by 0
# ./spec/controllers/users/registrations_controller_spec.rb:19:in `block (4 levels) in <top (required)>'
Can anyone suggest a reason this test is failing to create a user? The application works properly and an email arrives in my inbox when I use the users/sign_up
form.
./spec/controllers/users/registrations_controller_spec.rb
require 'rails_helper'
require 'factory_girl_rails'
RSpec.describe Users::RegistrationsController, type: :controller do
describe "POST #create" do
let(:valid_attributes) { FactoryGirl.build(:user_for_registration).attributes }
let(:invalid_attributes) { User.get_invalid_user.attributes }
let(:valid_session) { {} }
# Help Devise map routes from the test back to the original controller.
# See http://stackoverflow.com/questions/6659555/how-to-write-controller-tests-when-you-override-devise-registration-controller
before :each do
request.env['devise.mapping'] = Devise.mappings[:user]
end
context "with valid params" do
it "creates a new User" do
expect {
puts valid_attributes
post :create, {:user => valid_attributes}, valid_session
}.to change(User, :count).by(1)
end
end
end
end
./app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
# we have to explicitly permit params in overridden controllers like this
# see https://github.com/plataformatec/devise#strong-parameters
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) do |u|
u.permit(:name, :email, :password, :password_confirmation)
end
devise_parameter_sanitizer.permit(:account_update) do |u|
u.permit(:name, :email, :password, :password_confirmation, :current_password)
end
end
end
./spec/factories/users.rb
require 'ffaker'
FactoryGirl.define do
factory :user do |u|
@pass = "password"
name FFaker::Name.name
email { |me| "#{name.to_s.gsub(/\s/,'.')}_#{rand(1000).to_s}@testing.com" }
password @pass
factory :user_for_registration do
password_confirmation @pass
end
factory :user_for_account_update do
password_confirmation @pass
current_password @pass
end
end
end