Rails4 Rspec3でアプリケーション開発をしています。
テストコードとして以下のように記載しています。
lang
1 # 有効な属性の場合 2 context "with valid attributes" do 3 before{ 4 #blog_imdexを生成 5 @blog_image=FactoryGirl.build(:blog_image) 6 #ファイルが登録されるディレクトリを指定 7 @dir_path = "./public/images/#{Time.now.strftime("%Y")}/#{@blog_image.blog_index_id}/" 8 #登録する画像準備 9 IMG_FILE_NAME = 'test1.jpg' 10 @img_file_path = 'file/'+IMG_FILE_NAME 11 #登録する画像準備 12 upload_file_path = @dir_path + IMG_FILE_NAME 13 #ファイル事前に削除 14 File.unlink(upload_file_path) if File.exist?(upload_file_path) 15 } 16 17##test1 18 subject { 19 post :create, 20 :blog_images => fixture_file_upload(@img_file_path, 'image/jpg') 21 } 22 23##test2 24 it { is_expected.to change(BlogImage, :count).by(1) } 25 26 it "saves the new blog_images in the database" do 27 expect{ 28 post :create, 29 :blog_images => fixture_file_upload(@img_file_path, 'image/jpg') 30 }.to change(BlogImage, :count).by(1) 31 end 32 end 33 end
test1とtest2でsubjectを使用しているだけで、内容としては同じ意味合いとして記載しているつもりです。
ですが、テストを実行するとtest1のみエラーとなります。
lang
1Failures: 2 3 1) BlogImagesController POST #create with valid attributes should change #count by 1 4 Failure/Error: it { is_expected.to change(BlogImage, :count).by(1) } 5 expected #count to have changed by 1, but was not given a block 6 # ./spec/controllers/blog_images_controller_spec.rb:33:in `block (4 levels) in <top (required)>'
subjectの使い方が誤っているからこのようになっていると思うのですが、
どのように使うのが正解なのでしょうか。
お教えいただけないでしょうか..
どうぞよろしくお願いします。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。