Rails 测试夹具用户的电子邮件显示为空
Rails test fixture user's email showing as null
我正在尝试测试登录系统是否正常工作。我用测试用户创建了一个用户装置。
test/fixtures/users.yml
dave:
name: Mr Dave
email: dave@dave.com
password_digest: <%= User.digest('password') %>
我已经为登录系统创建了一个单元测试,用于测试用户登录和注销。
test/integration/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 没有提取我添加到 fixtures 文件夹的用户?
我认为它应该为您指明正确的方向,如果您仍有问题,请告诉我
原因是 setup
在测试 class 之外,因此 运行 没有。将它移到 class 声明中,它将顺利运行。
class SomeTest < ActionDispatch::IntegrationTest
def setup
@user = users(:dave)
end
我正在尝试测试登录系统是否正常工作。我用测试用户创建了一个用户装置。
test/fixtures/users.yml
dave:
name: Mr Dave
email: dave@dave.com
password_digest: <%= User.digest('password') %>
我已经为登录系统创建了一个单元测试,用于测试用户登录和注销。
test/integration/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 没有提取我添加到 fixtures 文件夹的用户?
我认为它应该为您指明正确的方向,如果您仍有问题,请告诉我
原因是 setup
在测试 class 之外,因此 运行 没有。将它移到 class 声明中,它将顺利运行。
class SomeTest < ActionDispatch::IntegrationTest
def setup
@user = users(:dave)
end