Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of described_class #194

Open
fenec opened this issue Nov 23, 2017 · 2 comments
Open

Usage of described_class #194

fenec opened this issue Nov 23, 2017 · 2 comments

Comments

@fenec
Copy link

fenec commented Nov 23, 2017

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

I recently started to use described_class instead of explicitly stating the class name:

let(:obj) { described_class.new }
vs
let(:obj) { Klass.new }

I didn't find any guidelines about this, so I wanted to discuss the pros and the cons of each approach.
An advantage of the former is that it looks cleaner. And I believe that the name of the method is self-evident even for those who are not familiar with RSpec's syntax.
What do you think, guys?

@nicknish
Copy link

Great thought! I prefer to described_class as it helps reduce redundancy in your code.

Similar to subject, it helps DRY up your test suite. Imagine using Klass.new all over your spec suite. If you were to ever change the name of your class, you'd have to change it everywhere.

@akz92
Copy link
Member

akz92 commented Mar 20, 2020

I agree! It also makes the code more reusable as you don't need to worry with the new class's name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants