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