#257 Request Specs and Capybara
Request specs in RSpec are a great way to ensure the entire application stack is working properly. Here I also show how to use capybara with integrated JavaScript testing using Selenium.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
thanks for the great tutorial but i just get Could not find generator rspec:install. error ?
@/users/719 make sure you have the rspec gem in the development group, not just in the test group in your Gemfile.
Perhaps you have rspec included only in your test group, rather than in both the test and development groups in your gemfile, like in the 'cast.
any gems which have generators or command line tools of their own have to exist in the development environment as well so you can access them. otherwise you will get 'not found' errors.
There's also DSL, previously known as Steak, that helps to write request specs in more Cucumber-like way (focusing on user stories). It's built into Capybara master and works just fine.
A caveat here is that webmock and other net/http mockers will give you a hard time by interfering with your capybara requests with this setup. Suggestions exist to configure Selenium to use Curb instead.
Digging the blend of Rspec and Cucumber. This will come in handy.
I do integration testing with Rspec but instead I use Steak and Akephalos, I have created a demo https://github.com/boriscy/akephalos_steak to setup a rails app to test, it's a simple configuration. I have also created this tutorial in spanish http://www.boliviaonrails.com/2011/03/01/testeando-javascript-con-steak-y-akephalos/
You might want to still use transactions for any other tests (that way they're faster).
Something like this would work:
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
if example.metadata[:js]
DatabaseCleaner.strategy = :truncation
else
DatabaseCleaner.start
end
end
config.after(:each) do
DatabaseCleaner.clean
if example.metadata[:js]
DatabaseCleaner.strategy = :transaction
end
end
Thank you!
Javi, thanks! Had to figure it our myself, only to discover later that you have this snippet here for over 2 years. I went a bit further with optimization, so my code is as follows:
Is this significantly faster than Cucumber, or is it purely an aesthetic decision?
Incredible amount of information passed on!
I'd loved to see you implementing the link_to_function in the view before you wrote the spec for it and having it fail. I know you would have had two failures, firstly the problem with the missing feature and secondly the problem with the missing :js => true. I understand that for a cast, as packed as this one, that might have been too much.
However that is just so much the workflow, that happens to me all the time.
Hmm after all, maybe this cast, which I still consider one of the greatest for my own interest and benefits, might have been worth being split into two....
But I appreciate that great balance of simple, intermediate and advanced casts.
Thank you for the great work
Robert
Very interesting alternative to Cucumber. Reminds me of a new feature in Capybara that integrates RSpec acceptance tests, letting you write a test like this:
require 'acceptance/acceptance_helper'
feature "Articles", %q{
In order to have an awesome blog
As an author
I want to create and manage articles
} do
background do
Article.create!(:title => 'One')
Article.create!(:title => 'Two')
end
scenario "Article index" do
visit '/articles'
page.should have_content('One')
page.should have_content('Two')
end
end
For more info, see this excellent blog post: http://jeffkreeftmeijer.com/2011/acceptance-testing-using-capybaras-new-rspec-dsl/
In the past I used Cucumber with Capybara and Selenium to test my javascript. It was just like the setup in this episode, but then with Cucumber instead of Rspec. Selenium also started Firefox automatically to test the javascript. But I was wondering how I could test this on a server instead of my own machine. I mean most servers don't have a GUI and don't have Firefox installed.
Anyone know if I can test javascript also on a server using Selenium and Firefox (or another browser)?
Check out the Selenium IDE plugin for firefox. It's awesome for testing and you can use it against any website/server.
http://seleniumhq.org/download/
Hi Jeroen,
You can run your selenium tests on a server without a GUI in background by using a virtual frame buffer called Xvfb. There you will find usage information.
Hopefully I helped you. :)
Botond
@Jeroen:
Check out Xvfb
http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=xvfb
I've got my tests running on a linux server (ubuntu) with cucumber, capybara, xvfb and firefox. Working like a champ.
I'd also like to know if this is much faster than Cucumber -- that would be a huge win to me
You can still use transactional fixtures if you do the following in your spec helper file:
As per:
http://blog.yakitara.com/2011/03/use-transactionalfixtures-with-capybara.html
You can also use just one shared db connection as mentioned on:
platformatec-blog
That is great approach, thank for the link. Unfortunately, in my current project I have two databases, and some models are connects to second database, so this approach does not work.
This is a modified version I've used for projects that have multiple connections
Selenium so slow... I suggest use Thoughtbot's capybara-webkit https://github.com/thoughtbot/capybara-webkit
I'm not getting the port number in the url when firefox launches (only 127.0.0.1). Do I have to specify the port somewhere?
I'm looking for good rails3 opensource application which use capybara and rspec to write test
http://stackoverflow.com/questions/6392619/looking-for-examples-of-rails-3-integration-acceptance-tests-with-rspec
This doesn't seem to work with FactoryGirl. When I turn transactional fixtures off, all of my specs depending on FactoryGirl fail. :'(
Since this screencast I use the request specs for my development.
It works great for understanding of the function and as a direct mapping with use cases from RE.
The only issues I have is speed.
As for most of the specs I need a logged in user. This is therefore created in nearly every before block. Also, the user factory creates certain associated models as well (in this case a company.)
Is there a better/faster way to do it?
Would you use fixtures for those cases?
Ryan,
This is really awesome, thanks for the screencast. Testing JS functionality is a huge improvement for me -- however, it seems to come at a pretty steep speed cost.
Is there a way to configure the specs (or more specifically, to configure Guard) to NOT run JS specs by default? I would prefer to only run those when I run the entire test suite, or if I've "enabled" js testing somehow.
Because guard calls the request spec whenever I do any work on the controller or other request specs, and because the request spec now runs selenium to test in firefox, my guard setup is now painfully slow when I'm working on anything that has associated js tests...
Use poltergeist headless browser driver. It is similar to webkit-capybara, but it is built on top of PhantomJS and easier to setup (no need of Xvfb virtual frame buffer) and place this code to your spec_helper.rb to speed up the tests and to fix the multiple threads issue:
Note: If you are using Spork, place the code to "Spork.each_run" block.
Edit: Ooops. Ryan created an episode about PhantomJS 3 weeks ago. And he encourages to use this hack too ;-)
Great cast...Thanks Ryan.
Note that Capybara 2.0 no longer includes Capybara methods by default in your request specs.
+1
I was confused by the following error:
Since Capybara 2.0 one has to use folder
spec/features
Capybara commands don't work in folderspec/requests
anymore.Which I learned by your link. - Thanks!
but when we run the "rails g integration_test task" command it automatically generates the spec/requests folder so how to change it to spec/features folder? Please help
the .make is a method is a method from the machinist gem, just saying...