Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.