Rake测试错误 - ActiveRecord :: Fixture :: FormatError:“Ruby on Rails教程”作者:MICHAEL HARTL

时间:2015-05-22 21:16:32

标签: ruby-on-rails ruby activerecord

我正在阅读MICHAEL HARTL的“Ruby on Rails Tutorial”一书,在第8节中,我实施了几个TEST。现在当你启动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

2 个答案:

答案 0 :(得分:2)

看起来你的灯具有一些糟糕的格式。在测试之前加载灯具,如果出现问题,您将收到错误&#34; ActiveRecord :: Fixture :: FormatError:ActiveRecord :: Fixture :: FormatError:ActiveRecord :: Fixture :: FormatError&#34 ;。每次测试都会出现这种情况,因为每次测试都取决于灯具。

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

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

答案 1 :(得分:1)

您的灯具文件似乎格式错误。

Users.yml

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

尝试识别属性。

希望它有所帮助!