rspec / rspec-metagem

RSpec meta-gem that depends on the other components
https://rspec.info
MIT License
2.86k stars 235 forks source link

Trouble stubbing YAML.load_file #37

Closed faizaankhan closed 5 years ago

faizaankhan commented 5 years ago

Here's a snippet

The class where I'm loading YAML file

class FeatureStorageService
  FEATURES = YAML.load_file(Rails.root.join("config/features.yml"))
end

The test

describe FeatureStorageService do
  let!(:yaml)     { YAML.load_file(Rails.root.join("spec/factories/files/features.yml")) }

  before do
    allow(YAML).to receive(:load_file).and_call_original
    allow(YAML).to receive(:load_file).with(Rails.root.join("config/features.yml")).and_return(yaml)
  end
end

The spec doesn't load the file at "spec/factories/files/features.yml" instead loads the original one. I have cross checked the syntax so many times.

Also, the stubbing works fine for my controller spec which is accessing the same service(class shown here) with the same syntax.

Dependencies: gem 'rspec-rails', '~> 3.4.2' gem 'rails', '4.2.7'

JonRowe commented 5 years ago

Closing as wrong repo and duplicate of rspec/rspec-mocks#1272