cucumber vs. RSpec

后端 未结 3 1229
既然无缘
既然无缘 2021-02-01 17:30

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

3条回答
  •  孤街浪徒
    2021-02-01 17:52

    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.

提交回复
热议问题