Testing custom controllers and super-users

264 views
Skip to first unread message

Chris Cummer

unread,
Feb 6, 2012, 2:59:39 PM2/6/12
to Refinery CMS
I have a custom controller not associated with any engine that I'd
like to test. However without a superuser refinery intercepts all
calls to the controller's routes and redirects to the superuser
creation process.

in /spec/support/refinery/controller_macros.rb there's a method called
login_refinery_user which looks like it could be extended to
login_refinery_superuser but I don't know how to incorporate that
module and the associated factories (i.e. Factory(:refinery_user))
into my own tests.

Could someone point me in the right direction?

Cheers
Chris

Philip Arndt

unread,
Feb 6, 2012, 3:14:53 PM2/6/12
to refine...@googlegroups.com
You can find my implementation of login_refinery_superuser on the
master branch https://github.com/resolve/refinerycms/blob/master/testing/lib/refinery/testing/controller_macros/authentication.rb

Chris Cummer

unread,
Feb 6, 2012, 3:25:32 PM2/6/12
to Refinery CMS
Yep, and I've copied that into my controller_macros.rb file, however
I'm unclear on how exactly to get that to work with my controller's
spec. If I do the following:


describe WidgetsController do
include Refinery::ControllerMacros

def setup
login_refinery_superuser
...
end

describe '#featured' do
before { setup }

... tests etc...
end
end


It generates an "undefined method `before' for
#<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007f990c6996a8>"
error. I'd like to use the Refinery::ControllerMacros in my controller
spec, I'm just not clear about how to integrate them.


On Feb 6, 12:14 pm, Philip Arndt <par...@gmail.com> wrote:
> You can find my implementation of login_refinery_superuser on the
> master branchhttps://github.com/resolve/refinerycms/blob/master/testing/lib/refine...

Philip Arndt

unread,
Feb 6, 2012, 3:52:05 PM2/6/12
to refine...@googlegroups.com
Try placing it outside of the setup block :) it knows how to run itself.. for example: https://github.com/resolve/refinerycms/blob/master/authentication/spec/controllers/refinery/admin/users_controller_spec.rb#L4

Chris Cummer

unread,
Feb 6, 2012, 3:57:50 PM2/6/12
to Refinery CMS
Ah! That makes perfect sense. We're getting closer. I now get this
error:

"Factory not registered: refinery_superuser"

which suggests the user.rb factory isn't being found when rspec is
run... ?


On Feb 6, 12:52 pm, Philip Arndt <par...@gmail.com> wrote:
> Try placing it outside of the setup block :) it knows how to run itself.. for example:https://github.com/resolve/refinerycms/blob/master/authentication/spe...

Philip Arndt

unread,
Feb 6, 2012, 4:01:16 PM2/6/12
to refine...@googlegroups.com
Or that it doesn't exist in your version of Refinery. Here's the definition:

https://github.com/resolve/refinerycms/blob/master/authentication/spec/factories/user.rb#L27-L29

:)

Pete Higgins

unread,
Feb 6, 2012, 4:41:31 PM2/6/12
to refine...@googlegroups.com
Chris,

I usually work around the need for a refinery user by disabling the welcome screen.

This is what I have in a file in spec/suport in a project using a recent-ish version of edge refinery:

module Refinery
  module ApplicationController
    module InstanceMethods
      def just_installed?
        false
      end 
    end 
  end 
end

pete

Philip Arndt

unread,
Feb 6, 2012, 4:43:13 PM2/6/12
to refine...@googlegroups.com
Sure, you *could* circumvent Refinery. :P

Pete, do you think that we should do that by default?

Pete Higgins

unread,
Feb 6, 2012, 4:45:08 PM2/6/12
to refine...@googlegroups.com
Philip,

I think it makes for a nice first-time experience, but it does complicate a few things. Maybe add a setting to disable it so that monkeypatches aren't needed? This would also save so DB queries from running on every request.

pete

Philip Arndt

unread,
Feb 6, 2012, 4:46:05 PM2/6/12
to refine...@googlegroups.com
I mean when testing; we should, however, always prompt to create the first user on a normal situation.

Chris Cummer

unread,
Feb 6, 2012, 7:56:44 PM2/6/12
to Refinery CMS
I think disabling it is a reasonable default to have when the
Rails.env == test. Currently it is not immediately obvious that this
is the issue when running controller test.

Thanks for the work-around Pete, and thanks for the help Philip!
Reply all
Reply to author
Forward
0 new messages