2014-12-18 2 views
3

내가 루비 레일에 내 우편물과 테스트를 실행하고, 나는 다음과 같은 오류가 점점 오전 :잘못된 datetime 값입니다. 루비 레일에

*********-C02MGBVJFD57:myapp ************$ bundle exec rake test:mailers 

# Running: 

EE 

Finished in 0.110500s, 18.0995 runs/s, 0.0000 assertions/s. 

    1) Error: 
UserMailerTest#test_account_activation: 
ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect datetime value: '2014-12-18 01:07:49 UTC' for column 'activated_at' at row 1: INSERT INTO `users` (`name`, `email`, `password_digest`, `admin`, `activated`, `activated_at`, `created_at`, `updated_at`, `id`) VALUES ('Michael Example', '[email protected]', '$2a$04$hz6pvZctid6gZEuv0.qAe.0mEfbEjxso9GrXwC3yHRhIThIJ7Vx5m', 1, 1, '2014-12-18 01:07:49 UTC', '2014-12-18 01:07:49', '2014-12-18 01:07:49', 762146111) 



    2) Error: 
UserMailerTest#test_password_reset: 
ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect datetime value: '2014-12-18 01:07:49 UTC' for column 'activated_at' at row 1: INSERT INTO `users` (`name`, `email`, `password_digest`, `admin`, `activated`, `activated_at`, `created_at`, `updated_at`, `id`) VALUES ('Michael Example', '[email protected]', '$2a$04$XTAWn5P9kikgOXqxQgiJqOjP027p4HnucHZ6pth2aaLwolaFcehsm', 1, 1, '2014-12-18 01:07:49 UTC', '2014-12-18 01:07:49', '2014-12-18 01:07:49', 762146111) 


2 runs, 0 assertions, 0 failures, 2 errors, 0 skips 

나는, 내가 시간을 사용하고 있음을 볼 수 users.yml 파일 내 비품을 검사 할 때 존 루비 스탬프 :

michael: 
    name: Michael Example 
    email: [email protected] 
    password_digest: <%= User.digest('password') %> 
    admin: true 
    activated: true 
    activated_at: <%= Time.zone.now %> 

archer: 
    name: Sterling Archer 
    email: [email protected] 
    password_digest: <%= User.digest('password') %> 
    activated: true 
    activated_at: <%= Time.zone.now %> 

lana: 
    name: Lana Kane 
    email: [email protected] 
    password_digest: <%= User.digest('password') %> 
    activated: true 
    activated_at: <%= Time.zone.now %> 

mallory: 
    name: Mallory Archer 
    email: [email protected] 
    password_digest: <%= User.digest('password') %> 
    activated: true 
    activated_at: <%= Time.zone.now %> 

<% 30.times do |n| %> 
user_<%= n %>: 
    name: <%= "User #{n}" %> 
    email: <%= "user-#{n}@example.com" %> 
    password_digest: <%= User.digest('password') %> 
    activated: true 
    activated_at: <%= Time.zone.now %> 
<% end %> 

내 dB/seeds.rb 파일과 같이 보입니다 :

User.create!(name: "Example User", 
      email: "[email protected]", 
      password:    "foobar", 
      password_confirmation: "foobar", 
      admin: true, 
      activated: true, 
      activated_at: Time.zone.now) 

99.times do |n| 
    name = Faker::Name.name 
    email = "example-#{n+1}@railstutorial.org" 
    password = "password" 
    User.create!(name: name, 
       email: email, 
       password:    password, 
       password_confirmation: password, 
       activated: true, 
       activated_at: Time.zone.now) 
end 

편집 : 여기 시험은

,

테스트/우편물/user_mailer_test.rb :

require 'test_helper' 

class UserMailerTest < ActionMailer::TestCase 

    test "account_activation" do 
    mail = UserMailer.account_activation 
    assert_equal "Account activation", mail.subject 
    assert_equal ["[email protected]"], mail.to 
    assert_equal ["[email protected]"], mail.from 
    assert_match "Hi", mail.body.encoded 
    end 

    test "password_reset" do 
    mail = UserMailer.password_reset 
    assert_equal "Password reset", mail.subject 
    assert_equal ["[email protected]"], mail.to 
    assert_equal ["[email protected]"], mail.from 
    assert_match "Hi", mail.body.encoded 
    end 
end 

내가 잘못하고 있어요 뭔가가 있나요? 도와 줘서 고마워.

+0

마이클 하틀가 sqlite3를, 당신은 MySQL을 사용하는 경우 그러나 @Ruby_Pry의 대답은 정확 말했다 인용 SQL 값으로 변환합니다. – moeabdol

+0

필자의 경우,'Time :: DATE_FORMATS [: db]'를''% d/% m/% Y % H : % M : % S '' 대신에''date_time_formats ' % Y/% m/% d % H : % M : % S ''. –

답변

5

마이그레이션 파일에 activated_at가 datetime으로 입력되어 있습니까? Time.zone.now.to_datetime을 시도해보십시오.

+0

이 파일을 시드 파일에 추가하려고 시도했지만 여전히 동일한 오류가 발생합니다. 다른 아이디어? – user1072337

+0

다른 도움을 많이 주시면 감사하겠습니다. – user1072337

+0

사과가 잠시 보지 못했지만 해결이 끝났습니까? –

0

열의 유형이 'datetime'인 것으로 의심됩니다. 'Time.zone.now'대신 'DateTime.now.in_time_zone'을 사용하십시오.

0

동일한 오류가 발생했습니다. 하지만 모두 seeds.rbusers.ymlTimezone.now.to_datetimedatetime 부분을 업데이트하고 실행했습니다 :

$ bundle exec rake db:migrate:reset 
$ bundle exec rake db:seed 

을 한 후 모든 GREEN을 얻기 위해 가정

$ bundle exec rake test 

실행합니다.

1

은 또는 당신은 단순히기구에 고정 created_at 시간을 사용할 수 있습니다 : 나는 MySQL은 5.7.10 사용하고

created_at: "2015-08-15 20:53:32" 
1

. createdahat 및 updated_at 타임 스탬프를 포함하여 ModelName.connection.execute (SQL 문)를 사용하여 테이블에 레코드를 직접 삽입하려고 시도했을 때 비슷한 문제가 발생했습니다. 여기

은 나를 위해 일한 것입니다 :

'#{DateTime.now.strftime('%F %T')}' 

주변의 작은 따옴표를 포함해야합니다.

0

액티브 :: Base.sanitize는 루비 Time 객체를 가지고 제대로

ActiveRecord::Base.sanitize(DateTime.now) 
=> "'2016-11-15 09:47:54'"