Rails测试 - current_account导致控制器方法返回nil

时间:2012-10-19 02:10:41

标签: ruby-on-rails ruby-on-rails-3 automated-tests

我有以下控制器和测试。我一直在

class CirclesController < ApplicationController
  before_filter :authenticate

  # GET /circles
  # GET /circles.json
  def index
    @circles = current_account.circles.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @circles }
    end
  end
end

我正试图用

测试它
require 'test_helper'

class CirclesControllerTest < ActionController::TestCase
  setup do
    @circle = circles(:facebook)
  end

  test "should get index" do
    login_as(:paul)
    current_account(:paul)
    get :index
    assert_response :success

    assert_not_nil assigns(:circles)
  end
end

我一直收到以下错误:

test_should_get_index(CirclesControllerTest):
NoMethodError: undefined method `circles' for nil:NilClass

我明白这是因为我没有以某种方式设置current_account。所以我的问题是,我如何“模拟”current_account?

这是current_account

class ApplicationController < ActionController::Base
  protect_from_forgery

  protected

    # gets ID of current account
    def current_account
      return unless session[:account_id]
      @current_account ||= Account.find_by_id(session[:account_id])
    end

0 个答案:

没有答案