---
title: "Testing Sinatra with Capybara and Cucumber"
url: "https://courses.osherove.com/courses/tdd-and-bdd-in-ruby/testing-sinatra-with-capybara-and-cucumber.md"
canonical: "https://courses.osherove.com/courses/tdd-and-bdd-in-ruby/testing-sinatra-with-capybara-and-cucumber/"
tags: []
author: "Roy Osherove"
description: "Lesson 11 of 15 in TDD and BDD in Ruby (Day 4 - Acceptance Tests and Continuous Delivery), 2:46. Free video by Roy Osherove."
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Testing Sinatra with Capybara and Cucumber

1. [Courses](https://courses.osherove.com/#courses)
2. [TDD and BDD in Ruby](https://courses.osherove.com/courses/tdd-and-bdd-in-ruby/)
3. Day 4 - Acceptance Tests and Continuous Delivery

Lesson 11 of 15

Duration: 2:46 [Watch on YouTube](https://www.youtube.com/watch?v=_XZTyoszrms)

Learn how to use a gem like Capybara to test a simple sinatra application's main page.

## In this lesson

Roy Osherove writes a [Cucumber](https://osherove.com/blog/2013/3/29/acceptance-frameworks-vs-specification-apis.html "Acceptance Frameworks Vs. Specification APis (osherove.com)") feature for the main page of a Sinatra application, which should show the default greeting, and uses Capybara in the steps to visit the page and check its content. He sets Capybara up in an env.rb file in the support folder under features and checks that the page contains hello world rather than equals an exact string, which he finds more [maintainable](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-8 "Maintainability (The Art of Unit Testing, 3rd ed., ch. 8)").

## Transcript

425 words

[\[00:02\]](https://www.youtube.com/watch?v=_XZTyoszrms&t=2s) Okay. Now, now what we're going to do is we're going to try to record a to write the feature for a Sinatra application. So, this is the feature file using Sinatra with Capybara. Usually, that's not what a customer would write. They would just write I'd like to have a main page, please. Thank you. But my use case is to teach you Capybara and how to use it inside. The scenario is just a simple main page. when I go to the main page and I should see the default greeting and if we run this we get Sinatra steps that we need to implement.

[\[00:35\]](https://www.youtube.com/watch?v=_XZTyoszrms&t=35s) Now if we have the gem Capybara installed we can just use the two most basic usages of Capybara is to visit places and then to assert that the page has some kind of content either text or we can have say have has should have selector and we can literally use selectors like we would use jQuery selectors here or CSS selectors. We can also check for X path and other things. And notice that it says default greeting and not a string because the string of what a default greeting could mean could change over time.

[\[01:08\]](https://www.youtube.com/watch?v=_XZTyoszrms&t=68s) And so we would have to go and change all the features to have that string. So it makes it more [maintainable](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-8#ch08lev1sec2 "Writing maintainable tests (The Art of Unit Testing, 2nd ed., ch. 8)") when it's a bit more generic. To get Capybara working we need to have the Capybara gem installed and we need to have this folder right here support under features with a file called env.rb RB and in there we require a Capybara and capybara/cucumber in the world

[\[01:39\]](https://www.youtube.com/watch?v=_XZTyoszrms&t=99s) Configuration which is part of cucumber. We say that the copy app is the name space of my application once we have it. And this is what the application from Sinatra looks like. Just a hello RB right here. Just a class my Sinatra base. It's called my app. I just have a get that returns hello world. And notice that here I said hello world with the with a bang.

[\[02:11\]](https://www.youtube.com/watch?v=_XZTyoszrms&t=131s) But if we go back to the specs of the default greeting, I just say should have content hello world. So this is more like a string contains assert than a string equals which is much more maintainable than having that string equals something specific. And we we don't want to check the whole page HTML string. We just want to check that it contains specific things. That gives us a bit more flexibility into the future.

[\[02:42\]](https://www.youtube.com/watch?v=_XZTyoszrms&t=162s) Questions? Okay.

## Related

- [String comparisons and maintainability](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-2) · The Art of Unit Testing, 3rd ed., ch. 2

    The 3rd edition's section on string comparisons and maintainability, the reason the step checks that the page contains hello world instead of an exact match.

- [Exact outputs and ordering overspecification](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-8) · The Art of Unit Testing, 3rd ed., ch. 8

    Section on overspecifying exact outputs, the problem avoided by keeping the expected greeting generic.

### Also linked in this lesson

#### The Art of Unit Testing, 2nd ed.

- [Writing maintainable tests](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-8#ch08lev1sec2), ch. 8

#### osherove.com

- [Acceptance Frameworks Vs. Specification APis](https://osherove.com/blog/2013/3/29/acceptance-frameworks-vs-specification-apis.html)

[Previous lesson Acceptance Tests with Cucumber](https://courses.osherove.com/courses/tdd-and-bdd-in-ruby/acceptance-tests-with-cucumber/) [Next lesson Iteration Zero and Continuous Delivery](https://courses.osherove.com/courses/tdd-and-bdd-in-ruby/iteration-zero-and-continuous-delivery/)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
