Skip to content

Instantly share code, notes, and snippets.

@frankolson
Last active January 1, 2020 00:00
Show Gist options
  • Save frankolson/987c3d82c7ec9b0669d4e8011f31257e to your computer and use it in GitHub Desktop.
Save frankolson/987c3d82c7ec9b0669d4e8011f31257e to your computer and use it in GitHub Desktop.
Stubbing constants that are used as a side effect of a let! function
# Rails helper config stuff...
# Because `let!' is always run before any `before` calls, you have to manually
# overide the constant in the rails helper
SomeClass.send :remove_const, 'FILE_NAME'
SomeClass.const_set 'FILE_NAME', 'spec/fixtures/files/something/cool.yml'
class SomeClass
FILE_NAME = 'something/cool.yml'
attr_reader :file
def load_file
@file = YAML.load_file(FILE_NAME)
end
end
require 'rails_helper'
RSpec.describe SomeClass do
let(:some_class) { SomeClass.new }
let!(:load_file) { some_class.load_file }
it 'should now use the stubbed constant' do
expect(some_class.file).
to eq YAML.load_file('spec/fixtures/files/something/cool.yml')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment