rspec navigatable it_behaves_like/shared_examples使用lsp

发布于 2025-02-01 18:23:59 字数 573 浏览 4 评论 0原文

我有一个旧版项目,该项目使用共享_examples的特征很大,在实际规格和共享_examples实现之间导航非常不便。

目前,唯一的方法是使用“某个示例”名称在项目中全球搜索。

RSpec.shared_examples "some example" do |parameter|
  let(:something) { parameter }

  it "uses the given parameter" do
    expect(something).to eq(parameter)
  end
end

RSpec.describe SomeClass do
  # "some example" has to become *something*
  # I can click and navigate to(jump-to-definition)
  include_examples "some example", "parameter1"
end

我想将LSP/SolarGraph用于此类导航。

也许有人以前这样做,愿意分享他们的做法?

I have a legacy project that uses shared_examples feature a lot and it is very inconvenient to navigate between actual specs and shared_examples implementation.

For now, the only way to do it is to search globally within a project using "some example" example name.

RSpec.shared_examples "some example" do |parameter|
  let(:something) { parameter }

  it "uses the given parameter" do
    expect(something).to eq(parameter)
  end
end

RSpec.describe SomeClass do
  # "some example" has to become *something*
  # I can click and navigate to(jump-to-definition)
  include_examples "some example", "parameter1"
end

I would like to use LSP/Solargraph for this kind of navigation.

Perhaps anyone did this before and willing to share how they did it?

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

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

发布评论

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

评论(1

眼泪都笑了 2025-02-08 18:23:59

事实证明,这比我预期的要简单。

只需将您的示例名称作为字符串常数提取,然后将其放在rspec.shared_examples实现旁边的某个地方即可。

# spec/support/shared_examples.rb
# in case you prefer one-liner use:
# RSpec.shared_examples(A_COLLECTION = 'a collection') do
# otherwise:

A_COLLECTION = 'a collection'
RSpec.shared_examples A_COLLECTION do
  let(:collection) { described_class.new([7, 2, 4]) }

  context 'initialized with 3 items' do
    it 'says it has three items' do
      expect(collection.size).to eq(3)
    end
  end
end
# spec/array_spec.rb
RSpec.describe Array do
  it_behaves_like A_COLLECTION
end

提示:如果它对您不起作用,请检查.solargraph.yml config,将“ spec/**/*”排除在默认情况下。

This turned out simpler than I expected.

Just extract your example name as a string constant and put it somewhere next to RSpec.shared_examples implementation.

# spec/support/shared_examples.rb
# in case you prefer one-liner use:
# RSpec.shared_examples(A_COLLECTION = 'a collection') do
# otherwise:

A_COLLECTION = 'a collection'
RSpec.shared_examples A_COLLECTION do
  let(:collection) { described_class.new([7, 2, 4]) }

  context 'initialized with 3 items' do
    it 'says it has three items' do
      expect(collection.size).to eq(3)
    end
  end
end
# spec/array_spec.rb
RSpec.describe Array do
  it_behaves_like A_COLLECTION
end

HINT: In case it doesn't work for you, check .solargraph.yml config which excludes "spec/**/*" from indexing by default.

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