Last active
January 1, 2020 00:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeClass | |
FILE_NAME = 'something/cool.yml' | |
attr_reader :file | |
def load_file | |
@file = YAML.load_file(FILE_NAME) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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