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的實例上調用它。因此,錯誤。


注意!

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



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