【问题标题】:Rake test error - ActiveRecord::Fixture::FormatError: "Ruby on Rails Tutorial" by MICHAEL HARTLRake 测试错误 - ActiveRecord::Fixture::FormatError: "Ruby on Rails Tutorial" by MICHAEL HARTL
【发布时间】:2015-05-23 06:23:14
【问题描述】:

我正在关注 MICHAEL HARTL 的“Ruby on Rails 教程”一书,在第 8 节中,我实现了几个测试。现在,当您启动 bundle exec rake test 时出现 26 个错误,我不明白什么不好。

ERROR["test_invalid_signup_information", UsersSignupTest, 2015-05-22 11:12:19 +0200]
test_invalid_signup_information#UsersSignupTest (1432285939.41s)
ActiveRecord::Fixture::FormatError:           ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_valid_signup_information", UsersSignupTest, 2015-05-22 11:12:19 +0200]
test_valid_signup_information#UsersSignupTest (1432285939.42s)
ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_password_should_have_a_minimum_length", UserTest, 2015-05-22 11:12:19 +0200]
test_password_should_have_a_minimum_length#UserTest (1432285939.43s)
ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_email_addresses_should_be_saved_as_lower-case", UserTest, 2015-05-22 11:12:19 +0200]
test_email_addresses_should_be_saved_as_lower-case#UserTest (1432285939.43s)
ActiveRecord::Fixture::FormatError:           ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_email_validation_should_accept_valid_addresses", UserTest, 2015-05-22 11:12:19 +0200]
test_email_validation_should_accept_valid_addresses#UserTest (1432285939.43s)
ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_name_should_not_be_too_long", UserTest, 2015-05-22 11:12:19 +0200]
test_name_should_not_be_too_long#UserTest (1432285939.44s)
ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_password_should_be_present", UserTest, 2015-05-22 11:12:19 +0200]
 test_password_should_be_present#UserTest (1432285939.44s)
ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

 ERROR["test_authenticated?_should_return_false_for_a_user_with_nil_digest",  UserTest, 2015-05-22 11:12:19 +0200]
test_authenticated?_should_return_false_for_a_user_with_nil_digest#UserTest (1432285939.45s)
ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_email_addresses_should_be_unique", UserTest, 2015-05-22 11:12:19 +0200]
test_email_addresses_should_be_unique#UserTest (1432285939.45s)
ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_email_should_be_present", UserTest, 2015-05-22 11:12:19 +0200]
test_email_should_be_present#UserTest (1432285939.46s)
ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_email_should_not_be_too_long", UserTest, 2015-05-22 11:12:19 +0200]
test_email_should_not_be_too_long#UserTest (1432285939.46s)
ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_name_should_be_present", UserTest, 2015-05-22 11:12:19 +0200]
test_name_should_be_present#UserTest (1432285939.46s)
ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_should_be_valid", UserTest, 2015-05-22 11:12:19 +0200]
test_should_be_valid#UserTest (1432285939.47s)
ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_login_with_remembering", UsersLoginTest, 2015-05-22 11:12:19 +0200]    
 test_login_with_remembering#UsersLoginTest (1432285939.47s)
ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


 ERROR["test_login_without_remembering", UsersLoginTest, 2015-05-22 11:12:19 +0200]
 test_login_without_remembering#UsersLoginTest (1432285939.48s)
 ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


 ERROR["test_login_with_valid_information_followed_by_logout", UsersLoginTest, 2015-05-22 11:12:19 +0200]
 test_login_with_valid_information_followed_by_logout#UsersLoginTest (1432285939.48s)
 ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


 ERROR["test_should_get_new", UsersControllerTest, 2015-05-22 11:12:19 +0200]
 test_should_get_new#UsersControllerTest (1432285939.49s)
 ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


 ERROR["test_current_user_returns_right_user_when_session_is_nil", SessionsHelperTest, 2015-05-22 11:12:19 +0200]
test_current_user_returns_right_user_when_session_is_nil#SessionsHelperTest (1432285939.50s)
  ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


 ERROR["test_current_user_returns_nil_when_remember_digest_is_wrong", SessionsHelperTest, 2015-05-22 11:12:19 +0200]
   test_current_user_returns_nil_when_remember_digest_is_wrong#SessionsHelperTest (1432285939.50s)
  ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


 ERROR["test_layout_links", SiteLayoutTest, 2015-05-22 11:12:19 +0200]
  test_layout_links#SiteLayoutTest (1432285939.51s)
  ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


 ERROR["test_should_get_new", SessionsControllerTest, 2015-05-22 11:12:19 +0200]
  test_should_get_new#SessionsControllerTest (1432285939.52s)
 ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


 ERROR["test_should_get_contact", StaticPagesControllerTest, 2015-05-22 11:12:19 +0200]
  test_should_get_contact#StaticPagesControllerTest (1432285939.52s)
 ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_should_get_help", StaticPagesControllerTest, 2015-05-22 11:12:19 +0200]
  test_should_get_help#StaticPagesControllerTest (1432285939.53s)
  ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_should_get_home", StaticPagesControllerTest, 2015-05-22 11:12:19 +0200]
  test_should_get_home#StaticPagesControllerTest (1432285939.53s)
 ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_should_get_about", StaticPagesControllerTest, 2015-05-22 11:12:19 +0200]
 test_should_get_about#StaticPagesControllerTest (1432285939.54s)
 ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError


ERROR["test_full_title_helper", ApplicationHelperTest, 2015-05-22 11:12:19 +0200]
 test_full_title_helper#ApplicationHelperTest (1432285939.54s)
 ActiveRecord::Fixture::FormatError:         ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError

这些是测试文件:

Users.yml

michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>

用户会话控制器测试

require 'test_helper'

class SessionsControllerTest < ActionController::TestCase
   test "should get new" do
     get :new
    assert_response :success
   end
 end

谢谢大家的帮助!


我编辑了 User.yml 文件,现在只有以下错误:


FAIL["test_current_user_returns_right_user_when_session_is_nil", SessionsHelperTest, 2015-05-22 11:12:20 +0200]
  test_current_user_returns_right_user_when_session_is_nil#SessionsHelperTest (1432285940.28s)

  --- expected
  +++ actual
  @@ -1 +1 @@
    -#<User id: 762146111, name: "Michael Example", email: "michael@example.com", password: nil, created_at: "2015-05-22 22:13:21", updated_at: "2015-05-22 22:13:22", password_digest: "$2a$04$PvQxwZgXnhi9RxICezCHJuQRRkUiSoP9qjPixSvx8mb...", remember_digest: "$2a$04$obYQeBWrZ59amJ9Ey2nd6.r8aeD.2h14uiEGB1XpywS...">
    +nil
    test/helpers/sessions_helper_test.rb:10:in `block in <class:SessionsHelperTest>'

会话助手测试

require 'test_helper'
class SessionsHelperTest < ActionView::TestCase

  def setup
    @user = users(:micheal)
    remember(@user)
   end

  test "current_user returns right user when session is nil" do
    assert_equal @user, current_user
    assert is_logged_in?
  end

  test "current_user returns nil when remember digest is wrong" do
    @user.update_attribute(:remember_digest, User.digest(User.new_token))
    assert_nil current_user
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby activerecord


    【解决方案1】:

    您的灯具格式似乎有些错误。夹具在测试之前加载,如果出现问题,您将收到错误“ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError”。这会出现在每个测试中,因为每个测试都依赖于固定装置。

    在 YAML 中,缩进很重要。在您的 Users.yml 文件中,您需要添加一点缩进,如下所示:

    michael:
      name: Michael Example
      email: michael@example.com
      password_digest: <%= User.digest('password') %>
    

    【讨论】:

    • 好的,现在 sono stati 的错误减少到三个,很好,我不知道为什么我没有注意到下降,因为我用谷歌搜索了这个错误,我意识到这是一个格式错误但我读过它是由空间引起的:,而我的是。但是我在上面添加了新错误。感谢您的帮助
    • 我想我明白了,我将用户 Micheal 添加到数据库中,现在只有一个错误,放在上面。
    • @LucaVasapolli 看起来您只有一次测试失败。显然 current_user 返回 nil,但期望它应该等于 @user。回顾本教程,确保您没有遗漏任何需要使current_user 方法工作的代码。
    • 我更正了错误,我调用了两次函数current_user,非常感谢
    【解决方案2】:

    您的夹具文件格式似乎有误。

    Users.yml:

    michael:
      name: Michael Example
      email: michael@example.com
      password_digest: <%= User.digest('password') %>
    

    尝试识别属性。

    希望对你有帮助!

    【讨论】:

    • 感谢您的帮助,主要错误就是这样,现在他们只有三个。
    猜你喜欢
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多