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



Rails 4中的Rspec flash集成測試錯誤:NameError:未定義的局部變量或方法`flash'for# - Rspec flash integration test error in Rails 4: NameError: undefined local variable or method `flash' for # Rails 3 +設計+ Rspec -控制器測試中未定義的方法錯誤 - Rails 3 + Devise + Rspec - undefined method error in Controller test rspec測試的問題,未定義的方法'post' - problem with rspec test, undefined method 'post' 使用rspec測試在PUT上使用未定義的方法“祖先” - undefined method `ancestors' on PUT with rspec test Rspec測試中的空類的未定義方法 - Undefined method for nil class in Rspec test RSpec錯誤“未定義方法‘respond_with’…” - RSpec error “undefined method `respond_with'…” Rails Rspec錯誤 - 未定義的方法`訪問' - Rails Rspec error - undefined method `visit' RSpec錯誤:未定義的方法`include?' for nil:nil的NilClass和undefined方法`downcase':NilClass - RSpec error: undefined method `include?' for nil:NilClass & undefined method `downcase' for nil:NilClass Factory_Girl + RSpec:在創建時未定義的方法'create'(:用戶) - Factory_Girl + RSpec: undefined method 'create' when create(:user) RSpec控制器測試失敗,Rails API -未定義方法“helper” - RSpec controller test failing with Rails API - Undefined Method 'helpers'
 
粤ICP备14056181号  © 2014-2020 ITdaan.com