Hommer Smith
Hommer Smith

Reputation: 27852

describe in rspec. When to use quotes?

Sometimes I see rspecs that look like this:

describe Contact do
...
end

and sometimes I see them like this:

describe 'Contact' do
...
end

What's the difference? And more generically, where can I find the docs for this?

Upvotes: 2

Views: 133

Answers (2)

Santhosh
Santhosh

Reputation: 29144

describe is a method which takes an argument and a block

Look at it this way

 describe('Contact', {})

If Contact is a class, you can pass it as a parameter like you do when you write your model specs.

describe(Contact, {}) # If Contact class doesn't exist, you will get an error

Upvotes: 1

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230411

If you have a specific class to describe, then you should use constant form. If you do, you will have described_class helper method that returns that class. Maybe come in handy in specs.

describe Contact do
  puts described_class # >> Contact
end

describe "Contact" do
  puts described_class # >> nil
end

Also, as @Santosh noted, constant form guards you from typos in class names. Say, you renamed a model, but forgot to update the specs. You'll get an error when running specs.

If this spec file is not focused on one class and is, for example, testing integration of several things, then you, naturally, don't use a constant. Because you don't have a definite one.

describe "user registration process" do
  # integration specs
end

Upvotes: 2

Related Questions