'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 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luis Arturo IƱiguez Hernandez