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

时间:2014-03-15 18:35:28

标签: 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;
+------------+--------------+------+-----+-------------+----------------+
| 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

但是,即使数据库中不存在内容字段,它也会被分配。这是lesson_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…">

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

0 个答案:

没有答案