如何测试从RSPEC变量/方法中隔离的导轨帮助者?

发布于 2025-02-09 03:58:05 字数 1997 浏览 2 评论 0原文

给定一个新的Rails项目,并添加了RSPEC-RAILS

给定以下(buggy)导轨助手(在app/helpers中):

module MyHelper
  def helper_method(_variable)
    variable.to_s
  end
end

给定规格:

require "rails_helper"

RSpec.describe MyHelper do
  let(:variable) { :test }

  it "succeeds" do
    expect(helper_method(variable)).to eq "test"
  end
end

此规范不会失败。尽管助手在其签名中具有_variable,但访问actible(在运行的Rails应用程序中将失败)。

由于RSPEC包括助手,因此从规格中可以访问Let(:variable),并愉快地使用它。

方式使RSPEC测试成为助手

有没有

require "rails_helper"

class MyHelperTest
  extend MyHelper
end

RSpec.describe "MyHelper" do
  let(:variable) { :test }

  it "succeeds" do
    expect(MyHelperTest.helper_method(variable)).to eq "test"
  end
end

办法以这种 一般问题(方法签名中的变量通常称为与测试设置相同的变量),这应该由RSPEC处理?


更新:使用Rails 7.0.3的复制脚本:

rails new helper-test
cd helper-test
bundle add rspec-rails
bundle

bin/rails g rspec:install
mkdir spec/helpers

cat > app/helpers/my_helper.rb <<EOF
module MyHelper
  def helper_method(_variable)
    variable.to_s
  end
end
EOF

cat > spec/helpers/my_helper_spec.rb <<EOF
require "rails_helper"

RSpec.describe MyHelper do
  let(:variable) { :test }

  it "succeeds" do
    expect(helper_method(variable)).to eq "test"
  end
end
EOF

bin/rails spec

更新2:以下规范会尽可能地失败。区别是直接调用helper_method直接调用helper_method

require "rails_helper"

RSpec.describe MyHelper do
  let(:variable) { :test }

  it "succeeds" do
    expect(helper.helper_method(variable)).to eq "test"
  end
end

仍然不确定为什么helper_method直接可用。

Given a new Rails project with rspec-rails added.

Given the following (buggy) Rails helper (in app/helpers):

module MyHelper
  def helper_method(_variable)
    variable.to_s
  end
end

Given the spec:

require "rails_helper"

RSpec.describe MyHelper do
  let(:variable) { :test }

  it "succeeds" do
    expect(helper_method(variable)).to eq "test"
  end
end

This spec does not fail. Although the helper has _variable in its signature, but accesses variable (which would fail in the running Rails app).

As RSpec includes the helper, the helper has access to let(:variable) from the spec and happily uses that.

Is there a way to make RSpec test a helper in such a way, that it is isolated from the test code?

I'm aware that I could do:

require "rails_helper"

class MyHelperTest
  extend MyHelper
end

RSpec.describe "MyHelper" do
  let(:variable) { :test }

  it "succeeds" do
    expect(MyHelperTest.helper_method(variable)).to eq "test"
  end
end

But it feels to me that this is such a general issue (often variables in method signatures are called the same as in the test setup) that this should be handled by RSpec already?


Update: Reproduction script using Rails 7.0.3:

rails new helper-test
cd helper-test
bundle add rspec-rails
bundle

bin/rails g rspec:install
mkdir spec/helpers

cat > app/helpers/my_helper.rb <<EOF
module MyHelper
  def helper_method(_variable)
    variable.to_s
  end
end
EOF

cat > spec/helpers/my_helper_spec.rb <<EOF
require "rails_helper"

RSpec.describe MyHelper do
  let(:variable) { :test }

  it "succeeds" do
    expect(helper_method(variable)).to eq "test"
  end
end
EOF

bin/rails spec

Update 2: The following spec fails as it should. The difference is calling the helper.helper_method instead of helper_method directly.

require "rails_helper"

RSpec.describe MyHelper do
  let(:variable) { :test }

  it "succeeds" do
    expect(helper.helper_method(variable)).to eq "test"
  end
end

Still not sure though why helper_method is available directly.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文