0
votes

I have a fairly simple rails app, based off of the first half of the railstutorial.org tutorial. I have a bit of head scratcher here... Capybara doesn't seem to be visiting the page i'm saying to visit. I want to make sure a user is logged in before they can see the products#index action. I have the 'not logged in' test passing fine, but the 'logged in' test keeps failing when it should be passing.

Here's my Product pages spec:

require 'spec_helper'

describe "Product pages" do

  subject { page }

  describe "index" do

    context "when not signed in" do
      before { visit products_path }

      it { should have_title 'Sign in' }
    end

    context "when signed in" do

      let(:user) { FactoryGirl.create(:user) }

      before do
        sign_in user
        visit products_path
      end

      it { should have_title('Listing all products') }

    end
  end 
end

the first test passes, but the second one doesn't. Using some trial and error i was able to figure out that it's going to the homepage (if i put the homepage's title in the should have_title() it passes)

Im pretty new to RSpec, but the user_pages_spec test that is pretty much identical is working a-ok:

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "index" do

    let(:user) { FactoryGirl.create(:user) }

    before do
      sign_in user
      visit users_path
    end

    it { should have_title('All users') }
    it { should have_content('All users') }
  end
end

what could be causing one to fail and one to pass? is there a way to have rspec spit out more information as to what's happening?

here's my gemfile, if that helps:

source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=micmanager

gem 'rails', '4.0.1'
gem 'pg', '0.15.1'
gem 'bootstrap-sass-rails'
gem 'bcrypt-ruby', '3.1.2'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9' 

group :development, :test do
  gem 'rspec-rails', '2.13.1'
  gem 'guard-rspec', '2.5.0'
  gem 'spork-rails', github: 'sporkrb/spork-rails'
  gem 'guard-spork', '1.5.0'
  gem 'childprocess', '0.3.6'
  gem 'guard-livereload', require: false
  gem 'rack-livereload'
  gem 'rb-fsevent', require: false
  gem 'factory_girl_rails', '4.2.1'
end

group :test do
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara', '2.1.0'
  gem 'growl', '1.0.3'
end

gem 'sass-rails', '4.0.0'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.0'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'rails_12factor', '0.0.2'
end

Any help is greatly appreciated! The only SO question i found like this was: Capybara visit method is not working which isn't any help at all.

thanks!

UPDATES

Products Controller:

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]
  before_action :signed_in_user
  before_action :admin_user, only: [:index, :new, :edit, :update, :destroy]
  # GET /products
  # GET /products.json
  def index
    @products = Product.all
  end
end

The before_actions that are mentioned, are in the sessions_helper file:

    module SessionsHelper

  def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.encrypt(remember_token))
    self.current_user = user
  end

  def signed_in?
    !current_user.nil?
  end

  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    remember_token = User.encrypt(cookies[:remember_token])
    @current_user ||= User.find_by(remember_token: remember_token)
  end

  def current_user?(user)
    user == current_user
  end

  def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    session.delete(:return_to)    
  end

  def store_location
    session[:return_to] = request.url if request.get?
  end

  def signed_in_user
    unless signed_in?
      store_location
      redirect_to signin_url, notice: "Please sign in."
    end
  end

  def correct_user
    @user = User.find(params[:id])
    redirect_to root_url unless current_user?(@user)
  end

  def admin_user
    redirect_to root_url unless current_user.admin?
  end
end

....and after posting all this, i figured it out... i paste the answer below. big h/t to Peter Goldstein for mentioning 'filters' since that's where the issue was...

1
Can you post the relevant pieces of your ProductsController? The index action as well as any filters? Do you have a controller spec that corresponds to this feature spec that is passing? - Peter Goldstein
Thanks man, your comment actually sent me in the right direction and i sorted this out. - afxjzs

1 Answers

0
votes

Found the solution:

the before_action :admin_user, only: [:index, :new, :edit, :update, :destroy] should haave been before_action :admin_user, only: [:new, :edit, :update, :destroy] since i don't want to restrict the index action to only admins but rather any logged in user. (originally i did want to do this, then changed my mind)

so it was failing on :admin_user and getting redirected to the home page, thus my error.

my bad.

what did we learn? Check your filters, kids. Always check your filters.