Topic: Testing with global variables set in ApplicationController

I'm using account_location and rspec.

I hope this isn't an easy answer, if so I've wasted too many hours on this :)

I'm using the account_location plugin to identify subdomains.  Here's my code:

application.rb

before_filter :find_account
..
protected
def find_account
  @account = Account.find_by_subdomain(account_subdomain)
end

projects_controller.rb
  def index
    @projects = @account.projects.find(:all)
   
    respond_to do |format|
      format.html { render :layout => "application" }
      format.xml  { render :xml => @projects.to_xml }
    end
  end

projects_controller_spec.rb
describe ProjectsController, "handling GET /projects" do
  before do
    @projects = mock(:projects)
    @account = mock_model(Account, :projects => @projects)
    @projects.stub!(:find).and_return(@projects)
  end
 
  def do_get
    login_as
    get :index
  end
 
  it "should be successful" do
    do_get
    response.should be_success
  end
end

So the problem is, is that it doesn't know what @account is, because it's not getting set in this scope.  How can I set that variable?  I tried assigns[:account] and stuff, but it doesn't work like that.

Thanks for any help!

Re: Testing with global variables set in ApplicationController

Where, specifically, is your spec failing. There are a number of methods you are depending on that aren't mocked or stubbed. Are you counting on AR to fill in there?

It might be as simple as stubbing find_account to return @account.

Last edited by cwd (2007-11-04 02:31:11)