如何重构 RSpec 文件中的辅助方法?

发布于 2024-12-07 11:31:08 字数 1207 浏览 0 评论 0原文

我正在使用 Ruby on Rails 3.1.0 和 rspec-rails 2 gem。我想重构以下代码(我故意省略了一些代码,并给出了有意义的名称以突出结构):

describe "D1" do
  # Helper method
  def D1_Method_1
    ...
  end

  context "C1" do
    # Helper methods
    def D1_C1_Method_1
      session.should be_nil # Note: I am using the RoR 'session' hash
      D1_Method_1           # Note: I am calling the 'D1_Method_1' helper method
      ...
    end

    def D1_C1_Method_2
      ...
    end


    it "I1" do
      D1_Method_1
      ...
    end

    it "I2" do
      ...
      D1_C1_Method_1
      D1_C1_Method_2
    end
  end

  context "C2" do
    # Helper methods
    def D1_C2_Method_1
      ...
    end

    def D1_C2_Method_2
      ...
    end


    it "I1" do
      D1_Method_1
      ...
    end

    it "I2" do
      ...
      D1_C2_Method_1
      D1_C2_Method_2
    end
  end
end

为了重构上述代码,我可以\应该做什么?

PS:我尝试在外部模块(名为Sample)中提取辅助方法,但是,例如与D1_C1_Method_1< /code> 方法(包含RoR session),当我运行规范文件时,出现以下错误:

Failure/Error: session.should be_nil
 NameError:
   undefined local variable or method `session' for Sample:Module

I am using Ruby on Rails 3.1.0 and the rspec-rails 2gem. I would like to refactor the following code (I have intentionally omitted some code and I have given meaningful names in order to highlight the structure):

describe "D1" do
  # Helper method
  def D1_Method_1
    ...
  end

  context "C1" do
    # Helper methods
    def D1_C1_Method_1
      session.should be_nil # Note: I am using the RoR 'session' hash
      D1_Method_1           # Note: I am calling the 'D1_Method_1' helper method
      ...
    end

    def D1_C1_Method_2
      ...
    end


    it "I1" do
      D1_Method_1
      ...
    end

    it "I2" do
      ...
      D1_C1_Method_1
      D1_C1_Method_2
    end
  end

  context "C2" do
    # Helper methods
    def D1_C2_Method_1
      ...
    end

    def D1_C2_Method_2
      ...
    end


    it "I1" do
      D1_Method_1
      ...
    end

    it "I2" do
      ...
      D1_C2_Method_1
      D1_C2_Method_2
    end
  end
end

What can\should I make in order to refactor the above code?

P.S.: I have tried to extract helper methods in an external module (named Sample) but, for example relating to the D1_C1_Method_1 method (that contains the RoR session), I get the following error when I run the spec file:

Failure/Error: session.should be_nil
 NameError:
   undefined local variable or method `session' for Sample:Module

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

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

发布评论

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

评论(1

一腔孤↑勇 2024-12-14 11:31:08

您是否尝试过将助手作为外部模块包含在内?

require 'path/to/my_spec_helper'

describe "D1" do
  include MySpecHelper
  ...
end

现在是助手:

# my_spec_helper.rb
module MySpecHelper
  def D1_C1_Method_1
    session.should be_nil
   ...
  end 
end

Have you tried to include the helpers as an external module?

require 'path/to/my_spec_helper'

describe "D1" do
  include MySpecHelper
  ...
end

And now the helper:

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