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

[英]Rspec Test Error - Undefined method create


Im trying to improve my Rspec tests and write more of them, In this example i can create an image fine and the test passes but i have an issue with deleting that image..

我试图改善我的Rspec测试并写下更多,在这个例子中,我可以创建一个图像罚款,测试通过,但我有一个删除该图像的问题..

Here is my test so far

到目前为止,这是我的测试

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

The error/failure i get is

我得到的错误/失败是

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)>'

How do i approach this test to delete the image

如何进行此测试以删除图像

Any help appreciated

任何帮助赞赏

1 个解决方案

#1


3  

Replace

更换

@timetable.create!(
    photo: image
  )

with

@timetable= TimeTable.create!(
    photo: image
  )

create! is a class method not instance method. You are calling it on the instance of TimeTable. Hence, the error.

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

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2014/02/23/72030282ae8845c3b913c6fd8c8e17a4.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告