登录测试
$ bundle exec rake test TEST=test/integration/users_login_test.rb \
> TESTOPTS="--name test_login_with_valid_information"
应该通过,但是我收到错误消息:" ActiveRecord :: Fixture :: FormatError:" 3次。许多其他类似的帖子已经回答了检查.yml文件的建议,因为它需要查看空格而不是标签。我尝试直接粘贴到教程中的文件,并尝试了各种标签和空格的组合,但无法摆脱这个错误。这是我的/test/fixtures/users.yml文件:
#michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>
这是我的/test/integration/users_login_test.rb文件:
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "login with invalid information" do
get login_path
assert_template 'sessions/new'
post login_path, session: { email: "", password: "" }
assert_template 'sessions/new'
assert_not flash.empty?
get root_path
assert flash.empty?
end
test "login with valid information" do
get login_path
post login_path, session: { email: @user.email, password: 'password' }
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)
end
end
任何帮助都会非常感激,事情进展顺利,停下来感到沮丧!
答案 0 :(得分:0)
在 test / fixtures / users.yml 文件中:
#michael: name: Michael Example email: michael@example.com password_digest: <%= User.digest('password') %>
#
前面有一个不必要的michael
(评论)。
[参考:Rails教程repo]