添加User Model测试及相关fixtures

This commit is contained in:
刘惊坤 2016-01-09 00:45:40 +08:00
parent b9d1bb1e12
commit 5f9a72221f
2 changed files with 52 additions and 10 deletions

View File

@ -1,11 +1,13 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
user1:
id: 1
username: user1
admin: 1
password_digest: <%= Digest::SHA1.hexdigest('1234567890') %>
email: 1234567@qq.com
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
user2:
id: 2
username: user2
admin: 0
password_digest: <%= Digest::SHA1.hexdigest('1234567890') %>
email: 1234561@qq.com

View File

@ -0,0 +1,40 @@
require 'test_helper'
class UserTest < ActiveSupport::TestCase
fixtures :users
setup do
@user = User.find(1)
@newuser = User.new
end
test "test_user_create" do
@newuser.username = 'newuser'
@newuser.email = '1234568@qq.com'
@newuser.password = '1234567890'
@newuser.password_confirmation = '1234567890'
@newuser.admin = 1
assert @newuser.save
end
test "test_user_read" do
assert_instance_of User, @user
assert_equal 1, @user.id, "test User.id"
assert_equal true, @user.admin, "test User.admin"
assert_equal "user1", @user.username, "test User.username"
assert_equal "1234567@qq.com", @user.email, "test User.email"
end
test "test_user_update" do
@user.email = 'newemail@gmail.com'
assert @user.save, @user.errors.full_messages.join("; ")
@user.reload
assert "newemail@gmail.com", @user.email
end
test "test_user_delete" do
@user.destroy
assert_raise(ActiveRecord::RecordNotFound) { User.find(@user.id) }
end
end