Controller filters running twice in 1.1.4?

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:

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?

Thanks,
Matt


View this message in context:
http://www.nabble.com/controller-filters-running-twice-in-1.1.4--tp17629653p17629653.html
Sent from the rspec-users mailing list archive at Nabble.com.

On Tue, Jun 3, 2008 at 5:31 PM, Matt McNeil [email protected]
wrote:

def foo
results in this:

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

Pat M. wrote:

 do_get

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…

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


View this message in context:
http://www.nabble.com/controller-filters-running-twice-in-1.1.4--tp17629653p17637883.html
Sent from the rspec-users mailing list archive at Nabble.com.

Pat M. wrote:

 do_get

script/spec:4:
http://rubyforge.org/mailman/listinfo/rspec-users

Application.rb (and the method called by the before filter) only seem to
be
loaded twice when they’re being run with autotest/spec. To reproduce
what
I’m showing here:

$ rails -v
Rails 2.1.0

rails test
cd test
script/plugin install git://github.com/dchelimsky/rspec.git
script/plugin install git://github.com/dchelimsky/rspec-rails.git
script/generate rspec
rsg rspec_scaffold User name:string

then add the before_filter :foo to application.rb, the foo method to
users_controller.rb, and the example to users_controller_spec.rb.

then:

$ /opt/local/bin/ruby -S script/spec -O spec/spec.opts
spec/controllers/users_controller_spec.rb
…F

Spec::Mocks::MockExpectationError in ‘UsersController handling GET
/users
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.252162 seconds

35 examples, 1 failure


View this message in context:
http://www.nabble.com/controller-filters-running-twice-in-1.1.4--tp17629653p17640917.html
Sent from the rspec-users mailing list archive at Nabble.com.

On Tue, Jun 3, 2008 at 7:27 PM, Matt McNeil [email protected]
wrote:

scaffold and am also seeing the same behavior here too.

Mock ‘UsersController’ expected :foo with (any args) once, but received


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

Do you require application.rb from anywhere, but including the whole
path? i.e. require File.join(RAILS_ROOT, “app”, “controllers”,
“application”). If you require the same file twice, but using two
different paths, then it’ll be loaded twice.

This may happen from a plugin you’ve got loaded as well.

Another thing to check is the paths in your generated specs. I
changed those a couple weeks ago, but to avoid this type of problem.
I may have screwed up, or perhaps you’ve got a mix of requires from
older gen’d specs and newer ones.

Pat

On Wed, Jun 4, 2008 at 4:29 AM, Pat M. [email protected] wrote:

This may happen from a plugin you’ve got loaded as well.

I’ve been seeing the same problem. In my case the symptom was a
warning about redefining a constant I have in ApplicationController.

I checked across my application and the only places I see application
being required (outside vendor/rails) are script/spec_server (and it’s
template) and rspec-rails/lib/spec/rails.rb.

Another thing to check is the paths in your generated specs. I
changed those a couple weeks ago, but to avoid this type of problem.
I may have screwed up, or perhaps you’ve got a mix of requires from
older gen’d specs and newer ones.

I’m not sure what “paths in your generated specs” means to check this
myself.

Regards,

Matt