Rails RSpec、DRY 规范:共享示例与辅助方法与自定义匹配器

Bru*_*cca 5 ruby rspec ruby-on-rails rspec-rails

我对控制器规范中的每个 HTTP 方法/控制器操作组合重复了一次以下测试:

it "requires authentication" do
  get :show, id: project.id
  # Unauthenticated users should be redirected to the login page
  expect(response).to redirect_to new_user_session_path
end
Run Code Online (Sandbox Code Playgroud)

我找到了以下三种方法来重构它并消除重复。哪一个最合适?

共享示例

在我看来,共享示例是最合适的解决方案。但是,为了将 传递params给共享示例而必须使用块感觉有点尴尬。

shared_examples "requires authentication" do |http_method, action|
  it "requires authentication" do
    process(action, http_method.to_s, params)
    expect(response).to redirect_to new_user_session_path
  end
end

RSpec.describe ProjectsController, type: :controller do
  describe "GET show", :focus do
    let(:project) { Project.create(name: "Project Rigpa") }

    include_examples "requires authentication", :GET, :show do
      let(:params) { {id: project.id} }
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

辅助方法

这具有不需要块传递project.id给辅助方法的优点。

RSpec.describe ProjectsController, type: :controller do
  def require_authentication(http_method, action, params)
    process(action, http_method.to_s, params)
    expect(response).to redirect_to new_user_session_path
  end

  describe "GET show", :focus do
    let(:project) { Project.create(name: "Project Rigpa") }

    it "requires authentication" do
      require_authentication(:GET, :show, id: project.id )
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

自定义匹配器

有一个单行测试会很好。

RSpec::Matchers.define :require_authentication do |http_method, action, params|
  match do
    process(action, http_method.to_s, params)
    expect(response).to redirect_to Rails.application.routes.url_helpers.new_user_session_path
  end
end

RSpec.describe ProjectsController, type: :controller do
  describe "GET show", :focus do
    let(:project) { Project.create(name: "Project Rigpa") }

    it { is_expected.to require_authentication(:GET, :show, {id: project.id}) }
  end
end
Run Code Online (Sandbox Code Playgroud)

提前致谢。

Bru*_*cca 5

didroe 在这篇 Reddit 帖子中提供的建议让我认为将方法/操作调用 ( process) 放在共享代码中并不是一个好主意,因为它增加了复杂性(降低了可读性)并且实际上并没有减少代码重复。

在进行了更多搜索之后,我在Aaron Sumner所著的Everyday Rails Testing with RSpec一书中找到了我认为最好的选择(第 102 页)。

创建以下自定义匹配器:

# spec/support/matchers/require_login.rb
RSpec::Matchers.define :require_login do |expected|
  match do |actual|
    expect(actual).to redirect_to \
      Rails.application.routes.url_helpers.new_user_session_path
  end

  failure_message do |actual|
    "expected to require login to access the method"
  end

  failure_message_when_negated do |actual|
    "expected not to require login to access the method"
  end

  description do
    "redirect to the login form"
  end
end
Run Code Online (Sandbox Code Playgroud)

并对每个控制器的每个操作使用如下测试:

it "requires authentication" do
  get :show, id: project.id
  expect(response).to require_login
end
Run Code Online (Sandbox Code Playgroud)

expect(response).to redirect_to new_user_session_path在所有测试中重复相比,这种方法具有以下优点:

  • 提高了可维护性。如果我们最终不得不改变这个断言,我们就在一个地方改变它,而不是不得不改变几十或几百个测试。
  • 更好的失败信息。

你怎么认为?