请帮助纠正测试。
它的模型用户:
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
before_create :create_remember_token
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :diary_name, presence: true, uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
validates :name, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
has_secure_password
has_many :recalls
has_many :posts, dependent: :destroy
belongs_to :gender
has_attached_file :avatar, :styles => { :large => "300x300>", :medium => "100x100>", :thumb => "30x30>" }
validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
validates_attachment_file_name :avatar, :matches => [/png\Z/, /jpe?g\Z/, /gif\Z/]
end
它是一个控制器UserController:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
flash.now[:error] = 'Invalid data'
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :diary_name, :password, :gender_id, :password_confirmation, :phone, :skype, :info, :avatar, :delete_avatar)
end
end
它是一个夹具用户:
one:
name: 'onfge'
email: 'MyStrdfing@ad.ad'
password_digest: '$2a$10$XS2HLwMZxg/7yRKAWd9AJ.afCMra0wGWK4b.FhkY/qo3Lmo/tKEiO'
remember_token: 'dc3461e13c8d316dad22332a503e06edafa0b9cb'
phone: '43535345'
skype: 'gggggg'
gender: one
info: 'MyString'
admin: true
avatar_file_name: nil
avatar_content_type: nil
avatar_file_size: nil
它的夹具性别:
one:
gender_name: '-'
two:
gender_name: 'male'
three:
gender_name: 'female'
不起作用的测试:
class UsersControllerTest < ActionController::TestCase
fixtures :users
setup do
@user = users(:one)
@input_attributes = {
email: 'yhyhyh@ad.ad',
gender_id: 1,
info: 'u76u67u67u',
name: 'fsdfsdf',
password: 'qwerty',
password_confirmation: 'qwerty',
phone: '435345345',
skype: 'sdggdfgdfgfdgd'
}
end
test "should create user" do
assert_difference('User.count') do
post :create,
user: @input_attributes
end
end
end
在控制台中运行测试后,我收到以下错误消息:
1)失败:UsersControllerTest#test_should_create_user [/home/kalinin/rails/ZSUM/test/controllers/users_controller_test.rb:37]: “User.count”未改变1.预期:3实际:2
这是记录的消息。很明显,交易因某些奇怪的原因而被回滚:
[1m[35mUser Load (0.1ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 980190962]]
[1m[36m (0.1ms)[0m [1mSELECT COUNT(*) FROM "users"[0m
Processing by UsersController#create as HTML
Parameters: {"user"=>{"email"=>"yhyhyh@ad.ad", "gender_id"=>"1", "info"=>"u76u67u67u", "name"=>"fsdfsdf", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "phone"=>"435345345", "skype"=>"sdggdfgdfgfdgd"}}
[1m[35m (0.1ms)[0m SAVEPOINT active_record_1
[1m[36mUser Exists (0.1ms)[0m [1mSELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('yhyhyh@ad.ad') LIMIT 1[0m
[1m[35mUser Exists (0.1ms)[0m SELECT 1 AS one FROM "users" WHERE "users"."diary_name" IS NULL LIMIT 1
[1m[36mUser Exists (0.1ms)[0m [1mSELECT 1 AS one FROM "users" WHERE LOWER("users"."name") = LOWER('fsdfsdf') LIMIT 1[0m
[1m[35m (0.1ms)[0m ROLLBACK TO SAVEPOINT active_record_1
答案 0 :(得分:0)
使用pry,在@ user.save之后将binding.pry语句插入控制器,再次运行测试,处理将打开控制台并停在pry语句,然后你可以查看@ user.errors看看什么错了。