Testing rSpec-如何为模型列编写失败的测试

Testing rSpec-如何为模型列编写失败的测试,testing,ruby-on-rails-4,rspec2,ruby-2.1,Testing,Ruby On Rails 4,Rspec2,Ruby 2.1,我正在尝试使用TDD开发我的应用程序,所以我需要先为数据库列编写一个失败的测试。然而,当我将内容添加到模型中不存在的字段时,它似乎无论如何都会被分配。我不知道如何使这次考试失败 Ruby 2.1.1、Rails 4.0.3、rSpec 2.14.1、FactoryGirl 4.4.0 我的模型如下所示: mysql> describe lesson_plans; +------------+--------------+------+-----+-------------+--------

我正在尝试使用TDD开发我的应用程序,所以我需要先为数据库列编写一个失败的测试。然而,当我将内容添加到模型中不存在的字段时,它似乎无论如何都会被分配。我不知道如何使这次考试失败

Ruby 2.1.1、Rails 4.0.3、rSpec 2.14.1、FactoryGirl 4.4.0

我的模型如下所示:

mysql> describe lesson_plans;
+------------+--------------+------+-----+-------------+----------------+
| Field      | Type         | Null | Key | Default     | Extra          |
+------------+--------------+------+-----+-------------+----------------+
| id         | int(11)      | NO   | PRI | NULL        | auto_increment |
| user_id    | int(11)      | NO   |     | NULL        |                |
| title      | varchar(255) | NO   |     | Lesson Plan |                |
| synopsis   | text         | YES  |     | NULL        |                |
| created_at | datetime     | YES  |     | NULL        |                |
| updated_at | datetime     | YES  |     | NULL        |                |
+------------+--------------+------+-----+-------------+----------------+
it "should have a content field" do
  lesson_plan = build(:lesson_plan, user: @user,
                      content: "The French Revolution was a period of radical social and         political upheaval in France from 1789 to 1799 that profoundly affected French and modern history...")
  puts lesson_plan.inspect
end
我想添加“内容”一栏。我的测试目前如下所示:

mysql> describe lesson_plans;
+------------+--------------+------+-----+-------------+----------------+
| Field      | Type         | Null | Key | Default     | Extra          |
+------------+--------------+------+-----+-------------+----------------+
| id         | int(11)      | NO   | PRI | NULL        | auto_increment |
| user_id    | int(11)      | NO   |     | NULL        |                |
| title      | varchar(255) | NO   |     | Lesson Plan |                |
| synopsis   | text         | YES  |     | NULL        |                |
| created_at | datetime     | YES  |     | NULL        |                |
| updated_at | datetime     | YES  |     | NULL        |                |
+------------+--------------+------+-----+-------------+----------------+
it "should have a content field" do
  lesson_plan = build(:lesson_plan, user: @user,
                      content: "The French Revolution was a period of radical social and         political upheaval in France from 1789 to 1799 that profoundly affected French and modern history...")
  puts lesson_plan.inspect
end
但是,即使数据库中不存在内容字段,也似乎正在分配该字段。以下是第十一课计划的输出:

#<LessonPlan id: nil, user_id: 455, title: "The French Revolution", 
  synopsis: "Background and events leading up to the French Revo...", created_at: nil,
  updated_at: nil, content: "The French Revolution was a period of radical socia…">
#

在列存在之前,如何使此测试失败?另外,为什么课程计划id为nil?

数据库.yml
中描述了至少两个DB:用于开发,用于测试。运行
描述课程计划时使用了什么数据库
?您以前是否为表课程计划创建了
内容
(可能您添加了列,然后将其回滚)?在model
LessonPlan
中是否有一些与内容相关的代码(例如setters)?PS
为什么课程计划id为nil
-因为您只是创建记录而没有创建,所以它与
LessonPlan.new(user:@user,…)
相同!我的测试数据库仍然包含我回滚时的列。非常感谢。