← Back to Upcase

Feature specs with Devise & Capybara


(Jon Seidel) #1

I’m building an app, using Devise, and adding specs for new features. However, I am unable to get signed in when writing a feature spec: Rspec can’t sign in; running in the browser works fine. I also have HTTP authentication on the site and have created some auth helpers for this and other applications. Pastie here.

This is the first time I’ve tried to create a feature spec in this app; the TDD workshop and integration tests convinced me this would be a Good Thing To Do.

I’m using Rails 3.2.13, RSpec 2.13.0, Capybara 2.1.0.

Actually, this is just the latest symptom of a more general problem I’ve run into: figuring out what can be done in which type of spec (integration, request, feature,…) and which version of which gem to use can be crazy-making.


(Paul Morganthall) #2

I had a similar situation … though I was using Minitest. I used this StackOverflow entry for ideas, and ended up adding Devise.mappings which fixed my sign_in problem. I’m not sure you have the same problem, but there are a number of hints in the answers and comments. Perhaps they’ll help.


(Jon Seidel) #3

Thanks, Paul…Much appreciated. Unfortunately that doesn’t help in my case which is a feature spec. I have a number of controller specs and I am able to sign_in just fine there using the AuthHelper modules I cobbled together from various sources and trial-and-error.

I also cloned the repo from Rails-Devise-Rspec-Cucumber tutorial and found it woefully lacking: there are only three specs written and they are simple controller/model specs which don’t do much. It’s really about setting things up, not using them. And that’s pretty much the extent of what Devise has to say about Rspec and Devise testing except for some very basic comments on controller testing.


(Jon Seidel) #4

Figured out how to work-around this issue by just using the Capybara DSL and avoiding the use of the Devise ‘sign_in’ method (i.e., just used visit, fill_in, click_on…). sign_in works just fine in controller specs; however, as I re-read the Capybara documentation, it’s pretty clear that the controller is not available and that’s where the Devise sign_in method is defined, I believe.


(George Brocklehurst) #5

Warden (which Devise is built on top of) has some test helpers that let you shortcut the login process.

More information here: https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara


(Jon Seidel) #6

Terrific, George… thanks. I’ll give that a whirl.