[rspec-users] RSpec controller methods testing

zombiebit enderyurt at gmail.com
Thu May 31 08:37:29 UTC 2012


*I am testing my controller and I run coverage "rcov" to learn how much I
covered the test. However I cannot test Ruby methods. For example, This is
my controller,*


class AuthenticationsController < ApplicationController

      def index
        @authentications = current_user.authentications if current_user
      end

      #create an account via omniauth
      def create
        omniauth = request.env["omniauth.auth"]
        authentication =
Authentication.find_by_provider_and_uid(omniauth['provider'],
omniauth['uid'])
        if authentication
          sign_in_and_redirect(:user, authentication.user)
        elsif current_user
          current_user.build_with_provider(omniauth)
          redirect_to authentications_url
        else
          user = User.new
          user.build_with_provider(omniauth)
          user.save!(:validate => false)
          sign_in_and_redirect(:user, user)
        end
      end

      #signout
      def destroy
        @authentication = current_user.authentications.find(params[:id])
        @authentication.destroy
        flash[:notice] = "Successfully destroyed authentication."
        redirect_to authentications_url
      end

end

*And I wrote a controller_spec to test it.*

require 'spec_helper'

describe AuthenticationsController do

  before(:each) do
    @current_user = mock_model(User, :id => 1)
    @authentications = mock_model(Authentication, :user_id => 1)
    Authentication.stub!(:current_user).and_return(@current_user)
    Authentication.stub!(:authentication).and_return(@authentications)
  end

  describe "Authentication" do
    it "should not current user if a current user does not exist" do
      subject.current_user.should_not be_nil
      redirect_to widgets_path
    end

    it "should not have a current_user" do
      redirect_to root_path
    end
  end

  def mock_auth(stubs={})
    @mock_auth ||= mock_model(Authentication, stubs).as_null_object
  end

  describe "Get authentications" do
    it "should get all authentications " do
      Authentication.stub(:all) { [mock_auth] }
      get :index
      assigns(:authentication) == @authentications
    end
  end

  describe "Post authentication" do
    it "find an authentication" do
      Authentication.stub(:find_by_provider_and_uid).with("twitter",
"12345").and_return(mock_auth)
    end
    it "authentication exists go user page" do

    end
  end
end


*I don not know how I can handle these methods. Do you have any idea?
*

--
View this message in context: http://ruby.11.n6.nabble.com/RSpec-controller-methods-testing-tp4975830.html
Sent from the rspec-users mailing list archive at Nabble.com.


More information about the rspec-users mailing list