0

我正在尝试使用 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

但看起来内容字段正在被分配,即使它在数据库中不存在。以下是 course_plan.inspect 的输出:

#<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 为零?

4

0 回答 0