DEV Community

Vasily Polovnyov
Vasily Polovnyov

Posted on • Edited on

4 2

RSpec & Rails: what not to write in `it`

1. Useless, general words that bring no value:

# bad: what value? for what?
it "adds certain value"

# good
it "snoozes for an extra 5 minutes"


# bad: which result is correct?
it "returns correct result"

# good
it "returns user's initials"


# bad: how exactly it fails?
it "fails"

# good
it "raises a not found error"


# bad: formatted how?
it "returns formatted string"

# good
it "returns time in 24-hour format"

# bad: what 'ok' means?
it "is ok"

# good
it "creates draft invoices"
Enter fullscreen mode Exit fullscreen mode

2. Implementation details:

# bad
it "changes @scheduled_on"

# good
it "reschedules campaign"


# bad
it "sets @todos"

# good
it "assigns todos to a given user"
Enter fullscreen mode Exit fullscreen mode

3. Lies:

it "returns time in 24-hour format" do
  expect(...).to eq "9:25"
end

it "strips leading zeroes" do
  expect(foo(" 9:25 ")).to eq "9:25"
end
Enter fullscreen mode Exit fullscreen mode

See also:
RSpec Style Guide
Better Specs

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (2)

Collapse
 
rjrobinson profile image
R.J. Robinson

Would you be able to expand on what to write in these cases? RSpec is supposed to be very human-readable, so you make a lot of statements on what not to write. Could you explain why it's terrible to write #it statements like the exampled, and then explain what would be the better alternative and why? I like the idea of your post, just looking for the value you are trying to add. Seems like an angry tweet currently.

Collapse
 
vasily profile image
Vasily Polovnyov

Could you explain why it's terrible to write #it statements like the exampled, and then explain what would be the better alternative and why?

Sure, please have another look at the updated post:
dev.to/vasily/rspec-rails-what-not...

I also added further reading: Better Specs and RSpec Style Guide.

What do you think?

I like the idea of your post, just looking for the value you are trying to add. Seems like an angry tweet currently.

Oh, I'm sorry. I didn't mean to make it angry or preachy. I'm not a native speaker, so I tried to make it short in order to avoid spelling and grammar mistakes :-)

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay