Ruby on rails rails在模型或控制器中初始化方法?Rails测试错误

Ruby on rails rails在模型或控制器中初始化方法?Rails测试错误,ruby-on-rails,ruby,testing,model,initialization,Ruby On Rails,Ruby,Testing,Model,Initialization,我是RubyonRails的新手,正在进行我的第一个项目。我不是以英语为母语的人,所以我在谷歌上查不到。首先,我想知道,如果你声明了一个初始化方法,对于如下类: def initialize(attributes = {}) @name = attributes[:name] @email = attributes[:email] end 或者说: class Person def initialize(name, age) @name, @age = name, age end en

我是RubyonRails的新手,正在进行我的第一个项目。我不是以英语为母语的人,所以我在谷歌上查不到。首先,我想知道,如果你声明了一个初始化方法,对于如下类:

def initialize(attributes = {})
@name = attributes[:name]
@email = attributes[:email]
end
或者说:

class Person
 def initialize(name, age)
  @name, @age = name, age
 end
end
我必须在控制器或相应类的模型中删除该方法吗

我们只在控制器中使用实例变量(@name或@age)是真的吗?我们在迁移文件中声明的模型的intance变量,对吗?比如:

class CreateStudents < ActiveRecord::Migration
 def up
  create_table :students do |t|
   t.string :prename, :limit => 100, :null=>false
   t.string :lastname, :limit => 100, :null=>false
   t.date :birthday, :null => false
   t.text :contact
   t.boolean :daz, :null => false, :default => false
   t.integer :form_id

   t.timestamps null: false
 end
end

def down
 drop_table :students
end
然后我尝试了一些类似的方法: 需要“测试助手”

class StudentTest < ActiveSupport::TestCase
  test "hello" do
    stud = Student.new
    assert_not stud.save
  end
end
class StudentTest < ActiveSupport::TestCase
  test "hello" do
    assert true
  end
end
class StudentTest
同样的错误。。。 有人知道为什么吗?:)
感谢您的帮助

您在迁移“pername”(null:false)时使用了db约束。
因此,在测试中调用stud.save时出现错误,因为您的stud试图用空pername字段将stud记录保存在db中。也许,最好使用有效的?代替save:)

Thx获取帮助,但是当我测试类似的东西时:测试“hello”do stud=Student.new stud.prename=“Gustav”stud.lastname=“Meyer”stud.birth=“30.05.1992”。到stud.add添加到表单(3,“A”)assert stud.save end我得到完全相同的错误
class StudentTest < ActiveSupport::TestCase
  test "hello" do
    assert true
  end
end