6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【RSpec】described_classの小さなサンプルメモ

Last updated at Posted at 2018-05-21

サンプルクラス

person.rb
class Person
  attr_accessor :name
  def initialize
    @name = "kure"
  end
end

class Person2
  attr_accessor :name
  def initialize
    @name = "kure2"
  end
end

サンプルRSpec

require 'person'

RSpec.describe Person do

  subject(:kure){ Person.new }
  # ★described_class で describeで宣言されたクラス(=Person)を取ってくる
  subject(:kure_by_described_class){ described_class.new }
  subject{ Person2.new }

  # オーソドックスな書き方
  it "name is kure" do
    person = Person.new
    expect(person.name).to eq("kure")
  end

  it "name is kure(described_class)" do
    expect(kure_by_described_class.name).to eq("kure")
  end

  it "name is kure(subject)" do
    expect(kure.name).to eq("kure")
  end

  it "name is kure2(subject)" do
    expect(subject.name).to eq("kure2")
  end
end
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?