RSpec metadata

我們的測試:

require "rails_helper"

RSpec.describe CoursesController, type: :controller do
  describe "GET index" do
    it "assigns @courses and render" do
      course1 = create(:course)
      course2 = create(:course)

      get :index

      expect(assigns[:courses]).to eq([course1, course2])
    end

    it "render template" do
      course1 = create(:course)
      course2 = create(:course)

      get :index

      expect(response).to render_template("index")
    end
  end
end

describe 的部份有:type: :controller,這個叫做 RSpec metadata。

RSpec 看到這個就知道這是 Controller Spec。而只要測試的位置放對了 spec/controllers/<controller names>_spec.rb,在 spec/rails_helper.rb 有一行配置:

config.infer_spec_type_from_file_location!

會自動幫我們的測試加上正確的 metadata(type: :controller)。

所以可以把這個拿掉:

require "rails_helper"

RSpec.describe CoursesController do
  describe "GET index" do
    it "assigns @courses and render" do
      course1 = create(:course)
      course2 = create(:course)

      get :index

      expect(assigns[:courses]).to eq([course1, course2])
    end

    it "render template" do
      course1 = create(:course)
      course2 = create(:course)

      get :index

      expect(response).to render_template("index")
    end
  end
end

results matching ""

    No results matching ""