你好社区我正在做一个简单的应用程序,用户可以注册(作为新用户),所以我试图用我的用户控制器创建一个新用户:
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
这是我的 rspec 测试 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
当我测试它时,它显示此错误:
失败:
1) UserController 创建新用户
Failure/Error: assigns[:users].should_not be_new_record
expected nil not to be a new record
# ./spec/controllers/user_controller_spec.rb:7
在 0.15482 秒内完成 1 个示例,1 个失败
最后这是我的模型
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