0

Im trying to develop my app using TDD so I need to write a failing test first for a database column. However, when I add content to a non-existent field in a model it seems to get assigned anyway. I don't know how to make this test fail.

Ruby 2.1.1, Rails 4.0.3, rSpec 2.14.1, FactoryGirl 4.4.0

My model looks like this:

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        |                |
+------------+--------------+------+-----+-------------+----------------+

I would like to add the column 'content'. My test currently looks like this:

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

But it looks like the content field is being assigned even though it does not exist in the database. Here's the output from 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…">

How do I make this this test fail until the column exists? Also, why is the lesson_plan id nil?

jbmoon
  • 129
  • 1
  • 11
  • 1
    you have at least two DB described in `database.yml`: for development, for tests. what DB was used when you run `describe lesson_plans`? Have you created `content` for table lessons_plans before (maybe you add column and then rollback it)? Do you have some code related to `content` in model `LessonPlan` (for example setters)? PS `why lesson_plan id nil` - because you just build record without creating, it is the same as `LessonPlan.new(user: @user, ...)` – gotva Mar 15 '14 at 20:26
  • That was it! My test database still contained the column from when I had rolled back. Thank you! – jbmoon Mar 15 '14 at 20:48

0 Answers0