简体   繁体   中英

How to test extended functionality after devise sign_up action?

Apparently I have a unique situation that I can't find help with anywhere. I'm trying to extend my sign up process to create extra models in my database. Here is a basic flow of what I'd like to do:

  • User Signs up with Email/Password (along with other model values)
  • System creates a 'User' model
  • System creates a 'Profile' model
  • System creates a 'Company' model
  • System creates an 'Account' model

The biggest challenge is that I'm using Devise and I can't seem to figure out a way to test this functionality with Rspec. Here is a quick view of me simply trying to test the 'Sign Up' method (which does not work:

describe "New Users" do
  describe "signing up" do
    it "should create a new user" do
      lambda do
        post :sign_up, :user => Factory.attributes_for(:user)
        response.should be_success
      end.should change(User, :count).by(1)
    end
  end
end

I get the following error:

1) UsersController New Users signing up should create a new user
 Failure/Error: post :sign_up, :user => Factory.attributes_for(:user)
 ActionController::RoutingError:
   No route matches {:user=>{:email=>"test@user.com", :password=>"secret", :password_confirmation=>"secret"}, :controller=>"users", :action=>"sign_up"}
 # ./spec/controllers/users_controller_spec.rb:14:in `block (5 levels) in <top (required)>'
 # ./spec/controllers/users_controller_spec.rb:13:in `block (4 levels) in <top (required)>'

The Devise routes configure my user sign up routes as follows:

new_user_registration GET    /users/sign_up(.:format)  {:action=>"new", :controller=>"devise/registrations"}

I've been pulling my hair out with this, and can't seem to find any help in this area. Perhaps I'm approaching it wrong, but I want to configure all the aspects of setting up a user's account (ie. their company defaults, profile settings, etc..) on the initial sign up form. First I need to just figure out how to test the sign up process.

Thanks in advance!

First of all the routes you should link to is, because devise are using a POST method for sign-up:

user_registration POST   /users(.:format)                   devise/registrations#create

Maybe you can try pass there syntax such as:

it "should create a new user" do
expect {
  post :create, user: FactoryGirl.attributes_for(:user)
  response.should be_redirect
}.should change(User, :count).by(1)
end

EDIT Ok then, I maybe figure out how to solve the route problem, I added a new controller:

UsersController < Devise::RegistrationsController 

and also added there method sign_up. Then in routes.rb I added this code:

devise_for :users
devise_scope :user do
 post "/sign_up", :to => "devise/registrations#create"
end

and finally this line before the test into the test:

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

But I have there problem with validations, but I think it is for my app because I just made it in new branch on existing system. Hope it could help you. Nevertheless, I think this approach is quite hack or not common. I would rather go with recommended Devise approach. You can always create an user dependencies with FactoryGirl viz this link . Hope it will help you.

BTW Sry for the nonsense post about sign in:) I was tired:)

Isn't it

post :users 

The new_user_registration route redirects to the page containing the form ( new user action), not the create user action. You should find something like in your routes:

user_registration POST /users {:action => "create", :controller => "devise/registrations"}

Check you rake routes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM