首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rspec测试错误-未定义的方法创建

Rspec测试错误-未定义的方法创建
EN

Stack Overflow用户
提问于 2014-02-23 17:46:29
回答 1查看 417关注 0票数 0

我试图改进我的Rspec测试,并编写更多的测试,在本例中,我可以创建一个很好的映像,并且测试通过了,但是删除该图像有一个问题。

这是我目前为止的测试

代码语言:javascript
复制
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

我遇到的错误/失败是

代码语言:javascript
复制
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)>'

如何处理此测试以删除图像

感谢你的任何帮助

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-23 18:10:03

替换

代码语言:javascript
复制
@timetable.create!(
    photo: image
  )

使用

代码语言:javascript
复制
@timetable= TimeTable.create!(
    photo: image
  )

create!是一个类方法而不是实例方法。您将在TimeTable实例上调用它。因此,错误。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21971939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档