Lesson 11 of 15

Testing Sinatra with Capybara and Cucumber

Duration: 2:46 Watch on YouTube (external link, opens in new tab)

Markdown version

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 (external link, opens in new tab) 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 (external link, opens in new tab).

Transcript

425 words

[00:02] (external link, opens in new tab) 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] (external link, opens in new tab) 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] (external link, opens in new tab) And so we would have to go and change all the features to have that string. So it makes it more maintainable (external link, opens in new tab) 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] (external link, opens in new tab) 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] (external link, opens in new tab) 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] (external link, opens in new tab) Questions? Okay.