Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rspec测试错误-未定义的方法创建_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

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

Ruby on rails Rspec测试错误-未定义的方法创建,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我试图改进我的Rspec测试并编写更多的测试,在这个例子中,我可以创建一个很好的图像,并且测试通过,但是我在删除该图像时遇到了一个问题 这是我到目前为止的测试 describe TimeTable do before(:each) do Paperclip::Attachment.any_instance.stub( :post_process ).and_return(true) Paperclip::Attachment.any_instance.stub(

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

这是我到目前为止的测试

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
我得到的错误/失败是

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)>'
时间表应删除时间表的图像
失败/错误:@timeline.create!(
命名错误:
nil:NilClass的未定义方法'create!'
#./spec/models/timeline_spec.rb:33:in'block(2层)in'
如何通过此测试删除图像

感谢您的帮助

更换

@timetable.create!(
    photo: image
  )

create!
是类方法而不是实例方法。您在
timeline
的实例上调用它。因此出现错误。

替换

@timetable.create!(
    photo: image
  )

create!
是类方法而不是实例方法。您在
timeline
的实例上调用它。因此出现错误。

替换

@timetable.create!(
    photo: image
  )

create!
是类方法而不是实例方法。您在
timeline
的实例上调用它。因此出现错误。

替换

@timetable.create!(
    photo: image
  )

create!
是类方法而不是实例方法。您在
timeline
的实例上调用它。因此,出现了错误