我试图改进我的Rspec测试并编写更多它们,在这个例子中,我可以创建一个图像,测试通过,但我有一个删除该图像的问题..
这是我到目前为止的测试
describe TimeTable do
before(:each) do
Paperclip::Attachment.any_instance.stub(
:post_process
).and_return(true)
Paperclip::Attachment.any_instance.stub(
:save
).and_return(true)
# This was a little harder - had to check the stack trace for errors.
Paperclip::Attachment.any_instance.stub(
:queue_all_for_delete
).and_return([])
end
it 'should save a image for TimeTable' do
image = Rack::Test::UploadedFile.new(
'spec/fixtures/rails.png', 'image/png'
)
@timetable = TimeTable.new(
photo: image
)
@timetable.save.should eq(true)
@timetable.photo_file_name.should eq('rails.png')
end
it 'should delete an image for TimeTable' do
image = Rack::Test::UploadedFile.new(
'spec/fixtures/rails.png', 'image/png'
)
@timetable.create!(
photo: image
)
expect { @timetable.destroy }.to change { TimeTable.count }.by(-1)
end
end
我得到的错误/失败是
TimeTable should delete an image for TimeTable
Failure/Error: @timetable.create!(
NoMethodError:
undefined method `create!' for nil:NilClass
# ./spec/models/timetable_spec.rb:33:in `block (2 levels) in <top (required)>'
如何进行此测试以删除图像
任何帮助表示赞赏
答案 0 :(得分:3)
替换
@timetable.create!(
photo: image
)
与
@timetable= TimeTable.create!(
photo: image
)
create!
是一个类方法而不是实例方法。您在TimeTable
的实例上调用它。因此,错误。