« Back to Index

RSpec described_class

View original Gist on GitHub

DescribedClass.rb

# https://www.relishapp.com/rspec/rspec-core/docs/metadata/described-class

RSpec.describe Fixnum do
  it "is available as described_class" do
    expect(described_class).to eq(Fixnum)
  end
end

module Foo
  module Bar
    class Baz
      def initialize(msg = "hello")
        puts msg
      end
    end
  end
end

describe Foo::Bar::Baz do
  subject { described_class.new("hi") }
  
  # The subject is implicitly a new instance of what's in the describe block.
  # But it wont know what to pass to the constructor, 
  # so in this example we override the implicit subject
end