Question

 FakeFS::File.should_receive(:chmod) 

What does it do? Reference to http://www.zlu.me/mocking/rails/rspec/carrierwave/fog/s3/2012/07/17/testing-carrierwave-with-fog.html

FULL SPEC:

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
Was it helpful?

Solution

It's saying that that particular class, FakeFS::File, during the course of that particular it, should have the .chmod method called on it.

So as long as at some point the code does:

FakeFS::File.chmod

Then that expectation will be satisfied. This is a good way to make sure certain code is being called, or to ignore certain code.

For example, if FakeFS::File.chmod takes a long time to execute, or would require a lot of code set up, you can just add this expectation.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top