Ruby1.9.3-p0を使用してRails3.2.1にアップグレードしたばかりで、Machinist2.0を使用しています。大規模なプロジェクトを更新する前に、すべてのテストに合格しました。私が抱えている問題は、rspecテストで「let」呼び出し内にブループリントを作成し、それをbeforedoブロックで参照する場合です。
let (:new_post) {Post.make!}
before do
Post.stub!(:new).and_return(new_post)
end
これは以前は機能していましたが、現在は次のエラーが発生します。
1) PostsController GET index assigns all posts as @posts
Failure/Error: let (:new_post) {Post.make!}
NoMethodError:
undefined method `title=' for nil:NilClass
# ./spec/support/blueprints.rb:22:in `block in <top (required)>'
# ./spec/controllers/posts_controller_spec.rb:37:in `block (2 levels) in <top (required)>'
# ./spec/controllers/posts_controller_spec.rb:40:in `block (2 levels) in <top (required)>'
これが私の青写真です:
require 'machinist/active_record'
Post.blueprint do
title {"Post"}
body {"hello world"}
end
今のところ、私の回避策は、before doブロック内のインスタンス変数を使用してそれらを作成することですが、rspecテストをよりクリーンに保つため、「let」呼び出しを使用すると便利です。