Question

I'm trying to implement a feature with rspec2 and rails3 , basically I have a Post model which I'm trying to test by creating a Post (I'm using device for authentication)

this is my feature

spec/acceptance/new_post_feature_spec.rb

require 'spec_helper'
require 'rspec/example_steps'

feature "Creating a new post" do
  include Devise::TestHelpers
  before(:each) do
    @request.env["devise.mapping"] = Devise.mappings[:user]
    user = FactoryGirl.create(:user)
    sign_in user
  end

  Steps "Add a new post" do
    page.visit "/posts"
    page.should have_content("New Post")
  end
end

But I'm getting this error

undefined method `env' for nil:NilClass

Following are the gems I'm using

gem "rspec-rails", "~> 2.0"
gem 'database_cleaner'
gem 'capybara'
gem "factory_girl_rails", ">= 4.1.0"
gem 'rspec-example_steps'

with Guard/Spork

any help would be appreciated

Was it helpful?

Solution

@request is a controller test variable. It's not what you want here. In fact, in capybara specs you don't need to specify the devise mapping and you cant use sign_in.

Capybara fires up a browser, you must tell it to go to your login page and sign in through the same steps that you would do in a browser.

Try this: (you may need to change some input and button names)

let(:user) { FactoryGirl.create(:user) }
before(:each) do
  page.visit new_user_session_path
  # note: use the email/password input names below if yours are different
  page.fill_in "user[email]", :with => user.email 
  page.fill_in "user[password]", :with => user.password
  page.click_button "Sign in"
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top