« Return to Thread: controller filters running twice in 1.1.4?

Re: controller filters running twice in 1.1.4?

by Matt McNeil :: Rate this Message:

Reply to Author | View in Thread

Pat Maddox wrote:
On Tue, Jun 3, 2008 at 5:31 PM, Matt McNeil <nabble@mattmcneil.org> wrote:
>
> since upgrading to 1.1.4 (I had been running without issue on a git
> snapshot), I'm noticing that my filter actions initiated from application.rb
> seem to be running twice.  I created a test rails project with the rspec
> scaffold and am also seeing the same behavior here too.
>
> Adding this code to application.rb:
>
>  before_filter :foo
>
>  def foo
>    "foo"
>  end
>
> and an example in users_controller_spec.rb:
>
>    it "should call foo via a before filter" do
>      controller.should_receive(:foo)
>      do_get
>    end
>
> results in this:
>
> 1)
> Spec::Mocks::MockExpectationError in 'UsersController handling GET
> /users.xml should call foo'
> Mock 'UsersController' expected :foo with (any args) once, but received it
> twice
> script/spec:4:
>
> Is any one else seeing this?

Can you put a print statement inside application.rb to see if the file
itself is being loaded multiple times?

Pat
_______________________________________________
Hi Pat,

Yes, application.rb appears to be getting loaded twice.  Things are fine when the before_filter is being called from the UsersController:

class UsersController < ApplicationController

  before_filter :foo

  puts "#{__FILE__} loaded " + Time.now.to_s


/opt/local/bin/ruby -S script/spec -O spec/spec.opts  spec/controllers/users_controller_spec.rb
/Users/matt/test/app/controllers/users_controller.rb loaded Tue Jun 03 19:20:44 -0700 2008
...................................

Finished in 0.256665 seconds

35 examples, 0 failures

however, when that filter macro is moved to application.rb:

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time

  before_filter :foo

  puts "#{__FILE__} loaded " + Time.now.to_s

here's what happens:

/opt/local/bin/ruby -S script/spec -O spec/spec.opts  spec/controllers/users_routing_spec.rb spec/controllers/users_controller_spec.rb
/Users/matt/test/app/controllers/application.rb loaded Tue Jun 03 19:23:32 -0700 2008
/Users/matt/test/app/controllers/application.rb loaded Tue Jun 03 19:23:32 -0700 2008
..........................................F.....

1)
Spec::Mocks::MockExpectationError in 'UsersController handling GET /users.xml should call foo via a before filter'
Mock 'UsersController' expected :foo with (any args) once, but received it twice
script/spec:4:

Finished in 0.2891 seconds

48 examples, 1 failure

 « Return to Thread: controller filters running twice in 1.1.4?