The RSpec Book 読書記録(2)
Rails に入ったとたんにはまりだしたw まず、19章で環境のセットアップが本書と異なる。 本書は Rails 3.0.0, cucumber-rails 0.3.2 となっており、自分は Rails 3.2.11, cucumber-rails は 1.3.0 をインストールした。 そうしたところ、web_step.rbが最新の cucumber には入っていないらしい。 20章から急に駆け足になる。序盤の丁寧な説明は全くなく、サンプルコードをダウンロードして後はよろしく見てくれというスタンスっぽい。ソースコードは http://pragprog.com/titles/achbd/source_code からダウンロードできる。 おかげでいろいろハマッタのでメモしておく。 attr_accessible が必要 Rails 3.2 あたりからセキュリティ強化のため、フォームからモデルのインスタンス変数にアクセスする場合は、明示的に attr_accessible をつける必要があるらしく追加した。 model/genre.rb `class Genre < ActiveRecord::Base has_and_belongs_to_many :movies attr_accessible :name end ` model/movie.rb `class Movie < ActiveRecord::Base has_and_belongs_to_many :genres attr_accessible :title, :release_year, :genres .... ` migrateに余計なカラムつけない has_and_belongs_to_many にタイムスタンプは不要となったので削る db/migrate/20130129180816_create_genres_movies.rb TYPO。。。 意気揚々とrake cucumberするもエラー。 Scenario: Create movie in genre # features/create_movie.feature:7 Given a genre named Comedy # features/step_definitions/genre_steps.rb:9 When I create a movie Caddyshack in the Comedy genre # features/step_definitions/movie_steps.rb:9 Then Caddyshack should be in the Comedy genre # features/step_definitions/movie_steps.rb:18 undefined method `movies' for #<Genre:0x007f839de03260> (ActionView::Template::Error) どうやら、genre.moviesでエラーになっている。TYPOしてしまってhas_and_belongs_to_many :moveis としてしまいエラーが発生していた movies に直した。 ...