如何测试从RSPEC变量/方法中隔离的导轨帮助者?
给定一个新的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论