cucumber vs. RSpec

2019-03-09 18:08发布

I want to start diving into BDD. I have never used TDD before and am not sure if I should start by learning RSpec and then jump to Cucumber or just go straight to using Cucumber.

I have been reading on the internet about both and it seems to me that Cucumber could be a 'replacement' for RSpec. Am I right or should be one used for certain things and the other one for others?

3条回答
Juvenile、少年°
2楼-- · 2019-03-09 18:42

Cucumber and RSpec are both used for BDD but have different uses.

Think of Cucumber as describing what happens from the user's perspective, through interaction with the web browser. So you can have steps like:

Given I'm not logged in
When I login
Then I should be on the user dashboard page

Pretty broad, but there's a lot going on under the hood there. Cucumber is good for making sure all these sort of high-level features and functionality are covered (e.g., that when your user logs in, they're taken to the right page). But it's not a good tool for testing lower-level code. That's where RSpec comes in.

Take the login example above. Your user may be logging in with an email address or username. You probably want to ensure the email address is valid or that the username is a certain length...or that the thing they're using to login with is unique. You'd do this in your User model with a validation.

It's a trivial example, but this is the kind of thing you'd test using RSpec (in your spec/models/user_spec.rb file). This is not something you'd test using Cucumber.

So bottom line is:

Cucumber for higher-level tests describing broad functionality as viewed from the user's perspective

RSpec for lower-level tests describing details for how your classes, methods, models, controller, etc should actually work.

This post actually does a really good job of explaining when to transition from one tool to another:

http://www.sarahmei.com/blog/2010/05/29/outside-in-bdd/

I also recommend "The RSpec Book" and "Rails Test Prescriptions" for good resources on both tools and testing in general.

P.S. - Not to confuse things, but you can actually use RSpec for the high-level stuff too. But some of that decision is a matter of which tool you prefer or maybe whether or not you're working with a non-technical client who would benefit more from Cucumber's user-friendly syntax for describing scenarios.

查看更多
神经病院院长
3楼-- · 2019-03-09 18:44

Kenton did a great job with this answer. Here is how the authors of RSpec and Cucumber see it:

We use Cucumber to describe the behavior of applications and use RSpec to describe the behavior of objects.

Although we use Cucumber to focus on high-level behavior and use RSpec to focus on more granular behavior, each can be used for either purpose.

查看更多
Rolldiameter
4楼-- · 2019-03-09 18:45

Yes, cucumber and rspec are both used for BDD.

I personally prefer Cucumber, but some people find it offputting, and prefer their tests to be less english, more code. Both are great tools, though.

查看更多
登录 后发表回答