ruby-on-railsrubyrspecstubbing

Rspec - Undefined method 'allow'


I am trying to stub the env variable to access Rails.env.production?

context 'Rails environment is production' do
  it 'returns the correct api production service' do
    allow(ENV).to receive(:[]).with('RAILS_ENV').and_return('production')
    expect(application.api_domain).to eql('https://api.some_url.com')
  end
end

but I always get this error

NoMethodError:
   undefined method `allow' for #<RSpec::ExampleGroups::CesidApplication::CesidApplication::RailsEnvironmentIsDevelopment:0x00007fcc48cec518>
   Did you mean?  all

Module method im testing

module Cesid
  module Application
    def api_domain
     uri = URI.parse(marketing_suite_url)
     domain = PublicSuffix.parse(uri.host)
     Rails.env.production? ? "https://api.#{domain.name}" : "https://api-#{domain.name}"
    end
  end
end

Rspec version

3.5.4

Any ideas?


Solution

  • Configure the expect syntax in spec_helper.rb like:

    # spec_helper.rb
    RSpec.configure do |config|
      config.expect_with :rspec do |c|
        c.syntax = :expect
      end
    end
    

    Then the allow will work