当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby on rails - Rspec error 'field' cant be blank, when given a not blank field

问题描述:

I have one very anoing error while testing my simple app's photo model.

my spec for photos

describe Photo do

before { @photo = Photo.create!(image: "5513770961_a73024a244_z.jpg", wraper: true, to_slider: true) }

subject { @photo }

it {should respond_to(:image)}

it {should respond_to(:wraper)}

it {should respond_to(:to_slider)}

it {should be_valid}

end

when this spec executes, it returns a strange error

Failure/Error: before { @photo = Photo.create!(image: "5513770961_a73024a244_z.jpg", wraper: true, to_slider: true) }

ActiveRecord::RecordInvalid:

Validation failed: Image can't be blank

My test db is prepared and loaded, don't simply can figure out this...

Thanks for help..)

Photo model:

class Photo < ActiveRecord::Base

attr_accessible :image, :gallery_id, :wraper, :to_slider

belongs_to :gallery

validates :image, presence: true

mount_uploader :image, ImageUploader

end

网友答案:

please just don't give the image name give the the full path like

make image attribute accessible

by adding this line to the model

attr_accessible :image

example: ---

before { @photo = Photo.create!(image: "#{Rails.root}/spec/images/5513770961_a73024a244_z.jpg", wraper: true, to_slider: true) }
分享给朋友:
您可能感兴趣的文章:
随机阅读: