RSPEC-未定义的方法'允许'

发布于 2025-01-20 08:30:52 字数 906 浏览 2 评论 0原文

我试图使ENV变量固执以访问 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

但是我总是会得到此错误

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

模块方法IM测试

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版本

3.5.4

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萝莉病 2025-01-27 08:30:52

配置期望语法在spec_helper.rb like中,

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

然后允许将工作

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文