[rspec-users] Testing a rails controller *outside* of spec/controllers

Nicolás Sanguinetti godfoca at gmail.com
Thu Mar 11 08:46:50 EST 2010


On Thu, Mar 11, 2010 at 11:37 AM, David Chelimsky <dchelimsky at gmail.com> wrote:
> 2010/3/11 Nicolás Sanguinetti <godfoca at gmail.com>:
>> On Thu, Mar 11, 2010 at 11:12 AM, David Chelimsky <dchelimsky at gmail.com> wrote:
>>> 2010/3/11 Nicolás Sanguinetti <godfoca at gmail.com>:
>>>> Scratch that, it works if you run just that file, if you run the whole
>>>> suite it doesn't. WTH.
>>>
>>> The fact that it works at all is an accident :)
>>
>> Hah, ok :)
>>
>> Why shouldn't two different types of specs work off the same file? (if
>> you care for a technical explanation :))
>
> I explained that in another email this thread that I sent about a
> minute before this one. Maybe google delivered them to you out of
> order?
>
> You can have different types in one file, but not in one nesting
> construct. Nested example groups are subclasses of the outer group, so
> it's sort of like this:
>
> class Outer < Spec::Example::ExampleGroup; end
> class Inner < Outer
>
> Obviously, things are going to get messy if you try to introduce a new
> type to the mix on the way down the hierarchy.

Ah, I thought that you were surprised at having the different types at
top-level on the same file passing with script/spec and failing with
rake spec -- as in "it should always fail" -- which is why I wanted an
explanation, since I didn't see why.

I guess it's some random setup thing in another part of the specs. In
any case, putting it in a different file makes it always pass (well,
as long as the code works :P)

Thanks for your time, both you and Scott ^^

> Make sense?
>
>> Since it works by moving it to another file, so I'm just doing that
>> (describe MyController, :type => :controller), so it's just more idle
>> curiosity than anything else, at this point :)
>>
>>>>
>>>> 2010/3/11 Nicolás Sanguinetti <godfoca at gmail.com>:
>>>>> Interesting:
>>>>>
>>>>> describe SomeModule do
>>>>>  describe "Bar", :type => :controller do
>>>>>    controller_name "blah"
>>>>>  end
>>>>> end
>>>>>
>>>>> Doesn't work, while
>>>>>
>>>>> describe SomeModule do
>>>>> end
>>>>>
>>>>> describe "Bar", :type => :controller do
>>>>>  controller_name "blah"
>>>>> end
>>>>>
>>>>> does.
>>>>>
>>>>> 2010/3/11 Nicolás Sanguinetti <godfoca at gmail.com>:
>>>>>> On Thu, Mar 11, 2010 at 5:10 AM, Scott Taylor <scott at railsnewbie.com> wrote:
>>>>>>>
>>>>>>> On Mar 11, 2010, at 1:26 AM, Nicolás Sanguinetti wrote:
>>>>>>>
>>>>>>>> We have an app that has "extensions" that are built as rails engines.
>>>>>>>> Each engine needs some code in the controllers, which we solved as
>>>>>>>> "include this module, call a method". In order to test it, though, we
>>>>>>>> don't want to "copy" the test for how that works into each engine, so
>>>>>>>> the idea is to write the tests once, in 'spec/lib/extensions_spec.rb',
>>>>>>>> where we are testing the other stuff that's common for all the
>>>>>>>> extensions (which are defined on lib/extensions.rb, thus the spec's
>>>>>>>> location)
>>>>>>>>
>>>>>>>> In order to do it, I'm declaring a 'sample' controller in the specs
>>>>>>>> (just include the module, call the method), and then writing a couple
>>>>>>>> specs for what we are doing. Problem is, I can't get rspec to
>>>>>>>> understand that that example group is a controller example groups.
>>>>>>>>
>>>>>>>> I have
>>>>>>>>
>>>>>>>>    class MyExampleController < ApplicationController
>>>>>>>>      include Extensions::ControllerHelpers
>>>>>>>>      call_awesome_method # this, among other things, defines an
>>>>>>>> instance method on the controller that I want to test
>>>>>>>>    end
>>>>>>>>
>>>>>>>> inside my spec, and after that I tried:
>>>>>>>>
>>>>>>>> 1) describe("controller helpers", :type => :controller) {
>>>>>>>> controller_name "my_example"; ... }
>>>>>>>>
>>>>>>>> It complains about controller_name not being a method of this dynamic
>>>>>>>> subclass of ActiveSupport::TestCase.
>>>>>>>
>>>>>>> That seems like a really strange error.  What is the actual stack trace?
>>>>>>
>>>>>> ./spec/lib/extensions_spec.rb:20: undefined method `controller_name'
>>>>>> for ActiveSupport::TestCase::Subclass_1:Class (NoMethodError)
>>>>>>        from $gem_path/rspec-1.3.0/lib/spec/example/example_group_methods.rb:183:in
>>>>>> `module_eval'
>>>>>>        from $gem_path/rspec-1.3.0/lib/spec/example/example_group_methods.rb:183:in
>>>>>> `subclass'
>>>>>>        from $gem_path/rspec-1.3.0/lib/spec/example/example_group_methods.rb:55:in
>>>>>> `describe'
>>>>>>        from $gem_path/rspec-1.3.0/lib/spec/example/example_group_factory.rb:31:in
>>>>>> `create_example_group'
>>>>>>        from $gem_path/rspec-1.3.0/lib/spec/dsl/main.rb:28:in `describe'
>>>>>>        from ./spec/lib/extensions_spec.rb:19
>>>>>>        from $gem_path/activesupport-2.3.5/lib/active_support/dependencies.rb:147:in
>>>>>> `load_without_new_constant_marking'
>>>>>>        from $gem_path/activesupport-2.3.5/lib/active_support/dependencies.rb:147:in
>>>>>> `load'
>>>>>>        from $gem_path/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:15:in
>>>>>> `load_files'
>>>>>>        from $gem_path/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:14:in `each'
>>>>>>        from $gem_path/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:14:in
>>>>>> `load_files'
>>>>>>        from $gem_path/rspec-1.3.0/lib/spec/runner/options.rb:133:in `run_examples'
>>>>>>        from $gem_path/rspec-1.3.0/lib/spec/runner/command_line.rb:9:in `run'
>>>>>>        from script/spec:10
>>>>>>
>>>>>> (sanitized it a bit)
>>>>>>
>>>>>>> What versions of the test-unit gem do you have installed?  What version of rails?  rspec?  If you uninstall the test-unit gem, will it work with the :type => :controller hash given to describe?
>>>>>>
>>>>>> This is using rails 2.3.5, rspec 1.3.0, and rspec-rails 1.3.2. I do
>>>>>> not have test-unit on this system, I have mocha 0.9.8 (though it
>>>>>> shouldn't be required in the environment by anyone here)
>>>>>>
>>>>>>> (I've noticed *very* strange things with the test-unit gem installed in the past - such as a test suite which was 100% green, using a hybrid of mocha + rspec mocks/stubs.  When the gem was uninstalled, half the test suite failed!)
>>>>>>>
>>>>>>>> After some time reading the source I figured that :type => :controller
>>>>>>>> pretty much just works for config.include/config.extend. So on to the
>>>>>>>> next thing:
>>>>>>>
>>>>>>> Have you taken a look at Spec::Example::ExampleGroupFactory?
>>>>>>
>>>>>> Ah, darn, I hadn't gotten that far into rspec's rabbit hole, and
>>>>>> missed the reference in rspec-rails. That's what you get for trying to
>>>>>> debug stuff at 4am :) So, :type => :controller should be enough, then.
>>>>>>
>>>>>> Still, doing:
>>>>>>
>>>>>> describe MyExampleController, :type => :controller do
>>>>>>  it "should do stuff" do
>>>>>>    controller.should_receive(:foo)
>>>>>>    controller.extension.bar
>>>>>>  end
>>>>>> end
>>>>>>
>>>>>> Fails with 'undefined local variable or method `controller' for
>>>>>> #<ActiveSupport::TestCase::Subclass_1::Subclass_4:0x1075ca230>'
>>>>>>
>>>>>> And replacing controller with @controller gives me 'undefined method
>>>>>> `bar' for nil:NilClass'. Which is pretty much the same results I got a
>>>>>> few hours ago before going to sleep :(
>>>>>>
>>>>>> …So :type => :controller is apparently not enough for rspec to pick up on this.
>>>>>>
>>>>>>> Not sure if this will help, but here's a patch that brynary and I banged out way back in rspec 1.1.9 to support spec/unit & spec/integration: (the usual disclaimer applies - all the good parts are his, all the bad parts are mine)
>>>>>>>
>>>>>>> http://gist.github.com/328919
>>>>>>
>>>>>> Thanks :)
>>>>>>
>>>>>>> That piece of the code might at least take you to a place where you can debug what's going on.
>>>>>>>
>>>>>>> Best,
>>>>>>>
>>>>>>> Scott
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> rspec-users mailing list
>>>>>>> rspec-users at rubyforge.org
>>>>>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>>>>>>
>>>>>>
>>>>>
>>>> _______________________________________________
>>>> rspec-users mailing list
>>>> rspec-users at rubyforge.org
>>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>>>
>>> _______________________________________________
>>> rspec-users mailing list
>>> rspec-users at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>>
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list