dポイントプレゼントキャンペーン実施中!

Ruby1.9.3とRails3.2.9でのテスト環境のモデルの単体テスト(unit)での質問です。
unitのuser_test.rb に下記の記述を書いて試験しているのですが、saveコマンドでのusersテーブル
への書き込みが、下記でnewしたuserの内容ではなく、fixturesフォルダの下の、users.ymlのテストデータの内容が書き込まれるようなのですが、動作はそれでよいのでしょうか?また、YMLデータではなくて下記のデータを書き込ませるsaveの書き方とかはあるのでしょうか?

test "user auth test" do
user = User.new(:name => "tanaka", :password => "pass", :password_confirmation => "pass")
user.save
assert User.auth("tanaka", "pass")
end

後、現在、usersテーブルのhashed_passwordフィールドに、モデル(user.rb)の中で、パスワードの
セッターを再定義するために、

class User < ActiveRecord::Base

  def password=(pwd)
  @password = pwd
   #
  return if pwd.blank?
  self.hashed_password = User.encrypt(@password)
  end

# 以下、省略
end

の記述を入れているのですが、現在何故か、YMLのデータが暗号化されないでそのまま入力されてしまっていますま。このメソッドの呼び出しは、上に書いたusersテーブルの内容またはYMLデータの書き込み時に、実行されると思っていいのでしょうか?(両方、またはどちらか?)

御教示願えればと思います。

A 回答 (1件)

>saveコマンドでのusersテーブルへの書き込みが、下記でnewしたuserの内容ではなく、fixturesフォルダの下の、users.ymlのテストデータの内容が書き込まれるようなのですが、動作はそれでよいのでしょうか?また、YMLデータではなくて下記のデータを書き込ませるsaveの書き方とかはあるのでしょうか?


回答:
モデルの単体テスト(unit)実行時、まず、fixturesフォルダの下の、users.ymlがテスト用DBに書き込まれます。次に、テスト用スクリプトのsaveで実行した内容が、書き込まれます。
但し、test "user auth test" do が完了した時点で、saveで書き込んだ内容は、消滅します。
(次のテストは、fixturesフォルダの下の、users.ymlのデータ読み込んだ状態で、開始します)
従って、実際には、saveで実行した内容はテストDBに書き込まれます。

>後、現在、usersテーブルのhashed_passwordフィールドに、モデル(user.rb)の中で、パスワードのセッターを再定義するために、・・・・の記述を入れているのですが、現在何故か、YMLのデータが暗号化されないでそのまま入力されてしまっていますま。このメソッドの呼び出しは、上に書いたusersテーブルの内容またはYMLデータの書き込み時に、実行されると思っていいのでしょうか?
回答:
user = User.new(:name => "tanaka", :password => "pass", :password_confirmation => "pass")
を実行したタイミングで実施されているはすです。但し、saveで実行した結果が消滅するために、実施されていないように見えるだけです。
以下、確認用のソースと実行結果です。(User.cryptは入力文字の後に:cryptを付加した文字を返す形で実装しています)

test/fixtures/users.yml
--------------------------------
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/ …

one:
name: MyName1
password: MyPass1
password_confirmation: MyPass1
hashed_password: MyPass1_hash

two:
name: MyName2
password: MyPass2
password_confirmation: MyPass2
hashed_password: MyPass2_hash
-----------------------------------------
test/unit/user_test.rb
--------------------------------------------
require 'test_helper'
class UserTest < ActiveSupport::TestCase
test "user auth test" do
users = User.all
p "====user auth test_before==="
users.each do |user|
p user.name
p user.hashed_password
end
user = User.new(:name => "tanaka", :password => "pass", :password_confirmation => "pass" )
assert user.save, 'Failure1 to save user!'
users = User.all
p "====user auth test_after===="
users.each do |user|
p user.name
p user.hashed_password
end
assert true, 'Error auth test'
end
test "user get test" do
user = User.new(:name => "AAAAA", :password => "BBBB", :password_confirmation => "BBBB" )
assert user.save, 'Failure2 to save user!'
users = User.all
p "===user get test_after==="
users.each do |user|
p user.name
p user.hashed_password
end
assert true, 'Error Success to get'
end
end
-----------------------------------------------
models/user.rb
-----------------------------------------
class User < ActiveRecord::Base
attr_accessible :hashed_password, :name, :password, :password_confirmation
def User.encrypt(pwd)
return pwd + ':encrypt'
end
def password=(pwd)
@password = pwd
return if pwd.blank?
self.hashed_password = User.encrypt(@password)
end
end
----------------------------------------------
実行コマンド
rake db:test:load
rake test:units TEST="test/unit/user_test.rb"
実行結果
---------------------------------------------------
# Running tests:

"====user auth test_before==="
"MyName2"
"MyPass2_hash"
"MyName1"
"MyPass1_hash"
"====user auth test_after===="
"MyName2"
"MyPass2_hash"
"MyName1"
"MyPass1_hash"
"tanaka"
"pass:encrypt"
."===user get test_after==="
"MyName2"
"MyPass2_hash"
"MyName1"
"MyPass1_hash"
"AAAAA"
"BBBB:encrypt"
.

Finished tests in 0.486803s, 4.1084 tests/s, 8.2169 assertions/s.

2 tests, 4 assertions, 0 failures, 0 errors, 0 skips
------------------------------------------------------
半角空白で投稿のため、インデントがずれています。
    • good
    • 0
この回答へのお礼

情報ありがとうございます。
unitテストでのsaveの動作、理解できました。

データが暗号化されない件は解決致しました。

ありがとうございました。

お礼日時:2013/01/22 23:25

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!