From 5f9a72221f9ab7dfd26ab04f9b4822511ba3853d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=83=8A=E5=9D=A4?= Date: Sat, 9 Jan 2016 00:45:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0User=20Model=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=8F=8A=E7=9B=B8=E5=85=B3fixtures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/fixtures/users.yml | 22 +++++++++--------- test/models/users_model_test.rb | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 test/models/users_model_test.rb diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 937a0c0..550cc21 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -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 diff --git a/test/models/users_model_test.rb b/test/models/users_model_test.rb new file mode 100644 index 0000000..31042c6 --- /dev/null +++ b/test/models/users_model_test.rb @@ -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 +