Rails:单元测试:ActiveRecord :: StatementInvalid:PG :: Error:ERROR:“created_at”列中的空值

时间:2012-12-03 22:33:59

标签: ruby-on-rails

您好我正在尝试为我的用户传递简单的单元测试

require 'test_helper'

class UsersTest < ActiveSupport::TestCase
  test "user is created" do
    user = User.create!(:name => "Edmund")
    assert_equal user.id, User.first.id
  end
end

但是,我运行rake test:units并收到错误:

1) Error:
test_user_is_created(UsersTest):
ActiveRecord::StatementInvalid: PG::Error: ERROR:  null value in column "created_at" violates not-null constraint
: INSERT INTO "messages" ("content", "read") VALUES ('MyText', 1)

(这是完整的日志)

Edmunds-MacBook-Pro:langexchange edmundmai$ rake test:units
NOTICE:  CREATE TABLE will create implicit sequence "languages_id_seq" for serial column "languages.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "languages_pkey" for table "languages"
NOTICE:  CREATE TABLE will create implicit sequence "locations_id_seq" for serial column "locations.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "locations_pkey" for table "locations"
NOTICE:  CREATE TABLE will create implicit sequence "messages_id_seq" for serial column "messages.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "messages_pkey" for table "messages"
NOTICE:  CREATE TABLE will create implicit sequence "users_id_seq" for serial column "users.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users"
Run options: 

# Running tests:

E

Finished tests in 0.666783s, 1.4997 tests/s, 0.0000 assertions/s.

  1) Error:
test_user_is_created(UsersTest):
ActiveRecord::StatementInvalid: PG::Error: ERROR:  null value in column "created_at" violates not-null constraint
: INSERT INTO "messages" ("content", "read") VALUES ('MyText', 1)
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:654:in `async_exec'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:654:in `block in execute'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:653:in `execute'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/database_statements.rb:277:in `insert_fixture'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/fixtures.rb:502:in `block (5 levels) in create_fixtures'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/fixtures.rb:501:in `each'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/fixtures.rb:501:in `block (4 levels) in create_fixtures'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/fixtures.rb:500:in `each'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/fixtures.rb:500:in `block (3 levels) in create_fixtures'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/fixtures.rb:492:in `each'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/fixtures.rb:492:in `block (2 levels) in create_fixtures'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/fixtures.rb:491:in `block in create_fixtures'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:527:in `disable_referential_integrity'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/fixtures.rb:476:in `create_fixtures'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/fixtures.rb:895:in `load_fixtures'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/fixtures.rb:849:in `setup_fixtures'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:407:in `_run__1860443591909891061__setup__836517370287596446__callbacks'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/testing/setup_and_teardown.rb:35:in `run'

1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
rake aborted!
Command failed with status (1): [ruby -I"lib:test" -I"/Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rake-10.0.2/lib" "/Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/rake-10.0.2/lib/rake/rake_test_loader.rb" "test/unit/**/*_test.rb" ]
Tasks: TOP => test:units
(See full trace by running task with --trace)

我的模特是:

class Users < ActiveRecord::Base
  attr_accessible :age, :gender, :name

  has_many :sent_messages, :class_name => "Messages", :foreign_key => 'sender_id'
  has_many :received_messages, :class_name => "Messages", :foreign_key => 'receiver_id'

  belongs_to :location
  belongs_to :language

  before_save { |user| if user.new_record? then create_remember_token end }


    private

        def create_remember_token
            self.remember_token = SecureRandom.urlsafe_base64
        end

end


class Messages < ActiveRecord::Base
  attr_accessible :content, :read

  belongs_to :sender, :class_name => "User", :foreign_key => 'sender_id'
  belongs_to :receiver, :class_name => "User", :foreign_key => 'receiver_id'
end

有人可以帮忙吗?当我只是创建一个简单的Messages时,我不明白为什么它在讨论User模型...

更新

测试/装置/ messages.yml

one:
  content: MyText
  read: 1

two:
  content: MyText
  read: 1

2 个答案:

答案 0 :(得分:5)

要查看的堆栈跟踪中的行是

/Users/edmundmai/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/database_statements.rb:277:in `insert_fixture'

在测试开始之前,它会读取包含测试将使用的样本数据的灯具。有可能你的夹具没有正确的数据(虽然我承认我会认为updated_atcreated_at属性将由Rails神奇地管理。)

答案 1 :(得分:1)

如果你从谷歌到这里。可能是您的fixture yaml文件中有created_atupdated_at列,并且它们设置为空白。我只是从我带注释的model.rb文件中复制所有列并转换为yaml格式后执行此操作。