Reputation: 247
hello community i'm doing an simple application which an user can be able to registrer (as a new user), so im trying to create a new user with my Usercontroller:
class UserController < ApplicationController
def create
@users = User.new
if @users.save
flash[:notice] = 'new user was successfully created.'
redirect_to posts_path
else
render :action => :new
end
end
def new
@user = User.new
end
end
here is my rspec test user_controller_spec:
require 'spec/spec_helper'
describe UserController do
it "create new user" do
get :create
assigns[:users].should_not be_new_record
end
end
when i test it, it show this error:
Failures:
1) UserController create new user
Failure/Error: assigns[:users].should_not be_new_record
expected nil not to be a new record
# ./spec/controllers/user_controller_spec.rb:7
Finished in 0.15482 seconds 1 example, 1 failure
for last here is my model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
has_many :cars
validates_presence_of :email
end
Upvotes: 0
Views: 1895
Reputation: 21810
One problem is this:
get :create
Rails' resource routing will not route GET requests to the create action, because the create action is meant to have side effects.
To test the create action you'll need to do:
post :create, "with" => { "some" => "params" }
When testing controllers, it's a good idea to tail log/test.log
; it'll tell you where the test request got routed, if anywhere.
Upvotes: 1
Reputation: 18784
The record isn't saving because it is not valid. You have a validation that prevents a record from being created without an email address.
Also, your create action has no way to take input from a form.
def create
@users = User.new(params[:user]) # the params[:user] is the part you are missing
if @users.save
redirect_to posts_path, :notice => 'new user was successfully created.'
else
render :action => :new
end
end
Then, in your test, make sure you assign an email address.
get :create, :user => { :email => '[email protected]' }
assigns[:users].should_not be_new_record
Upvotes: 1