我试图改进我的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)>'如何处理此测试以删除图像
感谢你的任何帮助
发布于 2014-02-23 18:10:03
替换
@timetable.create!(
photo: image
)使用
@timetable= TimeTable.create!(
photo: image
)create!是一个类方法而不是实例方法。您将在TimeTable实例上调用它。因此,错误。
https://stackoverflow.com/questions/21971939
复制相似问题