RSpec : Ruby On Rails : Different behavior of symbol/string in describe : TDD

Lets see the following example and understand how writing a ruby symbol instead a string make difference in describe block of RSpec.

RSpec.describe Applicant, type: :model do
  describe 'Validation' do
    it { is_expected.to validate_presence_of(:first_name) }
    it { is_expected.to validate_presence_of(:last_name) }
    it { is_expected.to validate_presence_of(:email) }
  end
end

Outputs
 should require first_name to be set
 should require last_name to be set
 should require email to be set

RSpec.describe Applicant, type: :model do
  describe :validation do
    it { is_expected.to validate_presence_of(:first_name) }
    it { is_expected.to validate_presence_of(:last_name) }
    it { is_expected.to validate_presence_of(:email) }
  end
end
Outputs:
 should require first_name to be set (FAILED - 1)
 should require last_name to be set (FAILED - 2)
 should require email to be set (FAILED - 3)

Conclusion: In the later example or code snippet, RSpec tried to find a class name :validation as subject. However, the the former example, ‘it’ block is expecting the class name in outer most describe block as subject.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s