插入到组表

时间:2015-08-11 17:45:07

标签: ruby ruby-on-rails-4

我刚开始接受测试。当我运行这个时:

rake test test / models / user_test.rb

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  test "should not save without an email address" do
    user = User.new
    assert_not user.save
  end
end

我收到以下错误:

1)错误: UserTest#test_should_not_save_without_an_email_address: ActiveRecord :: StatementInvalid:SQLite3 :: ConstraintException:NOT NULL约束失败:groups.name:INSERT INTO&#34; groups&#34; (&#34; created_at&#34;,&#34; updated_at&#34;,&#34; id&#34;)VALUES(&#39; 2015-08-11 17:31:07&#39;,&# 39; 2015-08-11 17:31:07&#39;,980190962)

这是 user.rb

class User < ActiveRecord::Base

  has_many :groups
  has_many :user_groups

  attr_accessor :password
  EMAIL_REGEX = /A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
  validates :password, :confirmation => true #password_confirmation attr
  validates_length_of :password, :in => 6..20, :on => :create
  validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX

  before_save :encrypt_password
  after_save :clear_password

  def encrypt_password
    if password.present?
      self.salt = Digest::SHA1.hexdigest("# We add {self.email} as unique value and #{Time.now} as random value")
      self.encrypted_password = Digest::SHA1.hexdigest("Adding #{self.salt} to {password}")
    end
  end

  def clear_password
    self.password = nil
  end

end

这是 test_helper.rb

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Add more helper methods to be used by all tests here...
end

据我所知,我没有任何回调或其他试图写入&#34;组&#34;表。我的&#34; groups.yml&#34;是默认的,但是如果我只测试这个模型那不重要,对吗?任何关于我可以开始寻找的帮助都将非常感激。谢谢!

1 个答案:

答案 0 :(得分:2)

test_helper.rb 正在设置我的所有灯具,但它们没有定义。评论&#34;固定装置:所有&#34;修好了。