0 Replies - 1952 Views - Last Post: 03 October 2012 - 06:21 PM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

How to debug rspec tests for requests?

Posted 03 October 2012 - 06:21 PM

I broke one of my tests when I rolled an authentication system. I'm really bad at rails tests, and they're hard to inquire about given how much code needs to be posted, but here.. goes...

Testing Context

Error: I'm getting a response 302 (please sign in error) where I should get 200.

(spec/requests/news_posts_spec.rb)
require 'spec_helper'

describe "NewsPosts" do
  
  describe "GET /news_posts when signed off" do
    
    it "tells noobs they need to be logged in!" do
      get news_posts_path
      response.status.should be(302)
    end
  end
  
  describe "GET /news_posts when signed on as admin" do
    
    before :each do
      user = Factory(:user)
      user.admin = true
      user.roles << Role.where(:name => "communications")
      user.save
      post '/signin', :email => user.email, :password => user.password
      
      #sign_on_as_admin
      #stub_admin_user
      #controller.stub(:check_authorization).and_return(true)
    end
    
    it "Works for admins!" do
      get news_posts_path
      response.status.should be(200)
    end
  end
  
end

(spec_helper.rb)
[code]
.
.
.
    def integration_sign_in(user)
      visit signin_path
      fill_in :email, :with => user.email
      fill_in :password, :with => user.password
      click_button
    end
    
    def sign_on_as_admin
      @user = Factory(:user)
      @user.admin = true
      @user.roles << Role.where(:name => "communications")
      @user.save
      
      integration_sign_in(@user)
    end
    
    def stub_admin_user
      @user = Factory(:user)
      @user.roles << Role.where(:name => "communications")
      @user.admin = true
      
      controller.stub(:current_user).and_return(@user)
    end
.
.
.




Implementation

(app/controllers/news_posts_controller.rb)
class NewsPostsController < ApplicationController
  before_filter :check_authorization
  .
  .
  .
end




The key here is the method 'check_authorization'.
(app/controllers/application.rb)
class ApplicationController < ActionController::Base
  before_filter :authenticate
  
  protect_from_forgery
  include SessionsHelper
  
  private
  def check_authorization
    unless current_user.can?(action_name, controller_name)
      flash[:error] = "You are not authorized to view the page you requested"
      redirect_to '/'
    end
  end
end




Nothing I do seems to fix the problem. Any input would be appreciated.
Edit: Oh, and so, I'm completly in the dark as to how to figure out why things aren't working. It would be nice to be able to puts breadcrums to the console window to help me figure out what's happening.

This post has been edited by NotarySojac: 03 October 2012 - 08:20 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1