私はRubyonRailsチュートリアルを行っており、リスト6.29までです。これは、(一見標準的な)ユーザー認証プロセスのテストについて説明しています。
私の問題は、user_spec.rbファイルの次の(編集された)部分を理解することです。
describe User do
before do
@user = User.new(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { @user }
describe "return value of authenticate method" do
before { @user.save }
let(:found_user) { User.find_by_email(@user.email) }
describe "with valid password" do
it { should == found_user.authenticate(@user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not == user_for_invalid_password }
specify { user_for_invalid_password.should be_false }
end
end
end
私の主な混乱は次の行です。
before { @user.save }
これは本当にテストユーザーをデータベースに保存しますか?パスワードの正確さをテストする前にこのテストユーザーを保存すると、テストが冗長になりませんか? ユーザーを(そのパスワードで)保存し、それでも同じパスワード(保存したばかりです!)を持っているかどうかを確認しているように見えます。誰かが私が間違っている理由を明確にすることができますか?