FakeFS :: File.should_receive(:chmod)做什么?

时间:2014-04-08 15:51:02

标签: ruby-on-rails ruby rspec chmod specifications

 FakeFS::File.should_receive(:chmod) 

它做什么?参考http://www.zlu.me/mocking/rails/rspec/carrierwave/fog/s3/2012/07/17/testing-carrierwave-with-fog.html

完整规格:

class TestFileUploader
  mount_uploader :file, FileUploader
end

describe FileUploader do
  include FakeFS::SpecHelpers

  context 'for non-production environment' do
    it 'should upload video clip to dev-bucket on s3' do
      FakeFS.activate!
      FakeFS::File.should_receive(:chmod) #this is needed or you will get an exception
      File.open('test_file', 'w') do |f|
        f.puts('foo') # this is required or uploader_test.file.url will be nil
      end
      uploader_test = TestFileUploader.new
      uploader_test.file = File.open('test_file')
      uploader_test.save!
      uploader_test.file.url.should match /.*\/dev-bucket.*/ #test to make sure that it is not production-bucket
      FakeFS.deactivate!
    end
  end
end

1 个答案:

答案 0 :(得分:3)

它说的是,在特定FakeFS::File的过程中,该特定类it应该在其上调用.chmod方法。

只要在某些时候代码确实如此:

FakeFS::File.chmod

那样的期望就会满足。这是确保调用某些代码或忽略某些代码的好方法。

例如,如果FakeFS::File.chmod需要很长时间才能执行,或者需要设置大量代码,则可以添加此期望值。

请参阅:https://www.relishapp.com/rspec/rspec-mocks/v/3-0/docs/message-expectations/expect-message-using-should-receive