English 中文(简体)
Mocking authlogic from shoulda with mocha
原标题:

I am having trouble mocking authlogic from shoulda.

I have the following test fixture:

class HomeControllerTest < ActionController::TestCase
  context "on GET to index" do
    setup do
      activate_authlogic
      UserSession.stubs(:current_user).returns( user_session )
      get :index
    end

    should respond_with :success
  end

  def user_session
    @user_session.stubs(:user).returns(Factory.build(:user))
    @user_session.stubs(:record)
    @user_session
  end
end

The problem is that the require_user method in ApplicationController is still not getting a current_user.

def current_user
  puts "#{defined?(@current_user)}"
  return @current_user if defined?(@current_user)
  @current_user = current_user_session && current_user_session.record
end

def require_user
  unless current_user
    store_location
    flash[:notice] = "You must be logged in to access this page"
    redirect_to login_url
    return false
  end
end

Can anyone see what is wrong?

最佳回答

I think the problem may be something in the final line in your current_user method.

You might want to split it down into a few lines to make it more readable and easier to diagnose which of the parts of that statement are not as you d expect in your test.

E.g.

@current_user = current_user_session
@current_user.record if @current_user
return @current_user
问题回答

It looks like your mock is a little confused; It s mocking a User object (but the instance variable you re using is called @user_session), and then stubbing a #record method on that user.

Also, you re mocking the current_user method on the UserSession controller, when it s actually defined on ApplicationController.

What you re trying to assert in your test is that a user must be logged in to access the Home#index page. In this test, you shouldn t focus on the UserSession object -- it s not as important. What is important is that if the system recognizes a user object has been retrieved and identified as the "current_user", they should be allowed to access the page.

The following spec might work out for you:

class HomeControllerTest < ActionController::TestCase
  context "on GET to index" do
    setup do
      activate_authlogic
      @user = Factory.build(:user)
      login_as_user(@user)

      @controller.should_receive(:current_user).and_return(@user)

      get :index
    end

    should respond_with :success
  end

  def login_as_user(user = Factory.build(:user))
    @controller.stubs(:current_user).returns(user)
  end
end

I m using the @controller variable that ActionController::TestCase makes available to you. It represents the instance of the current controller being tested.

An important thing to note here is that -- with the exception of line 4 -- this test now doesn t care what you re using to provide authentication functionality. So long as your application defines a current_user method that returns a User object or nil, you could be using AuthLogic, Devise or any other solution, and your test will continue to work as you might expect.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签