ログインシステムが正しく動作することをテストしようとしています。私はテストユーザーと一緒にユーザーフィクスチャを作成しました。Railsは、nullとして表示されるフィクスチャのユーザの電子メールを表示します。
テスト/器具/ users.yml
dave:
name: Mr Dave
email: [email protected]
password_digest: <%= User.digest('password') %>
Iは、ユーザログインおよびログアウトをテストログインシステムのためのユニットテストを作成しました。
テスト/統合/ site_layout_test.rb
require 'test_helper'
def setup
@user = users(:dave)
end
...
test "login with valid information followed by logout" do
get login_path
post login_path, params: { session: { email: @user.email,
password: 'password' } }
assert is_logged_in?
assert_redirected_to @user
follow_redirect!
assert_template 'users/show'
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(@user)
delete logout_path
assert_not is_logged_in?
assert_redirected_to root_url
follow_redirect!
assert_select "a[href=?]", login_path
assert_select "a[href=?]", logout_path, count: 0
assert_select "a[href=?]", user_path(@user), count: 0
end
end
私はNilClassための電子メールの方法が存在しないというエラーを取得し、テストを実行しようとします。
Error:
SiteLayoutTest#test_login_with_valid_information_followed_by_logout:
NoMethodError: undefined method `email' for nil:NilClass
test/integration/site_layout_test.rb:35:in `block in <class:SiteLayoutTest>'
はなぜRailsは私が備品フォルダに追加したユーザーを拾っていませんか?
テストファイル全体を投稿できますか? – Iceman
とにかく、私の推測では、 'setup'はテストスイートクラスの外にあり、実行されていないと考えられます。 – Iceman
あなたの推測は正しいです。それは可能であり、真実でもあります。 –