Forum: RSpec Testing controller plugin

Posted by Phillip Koebbe (pkoebbe)
on 2010-02-01 16:31
(Received via mailing list)
I have just recently started creating some plugins, mainly of the
controller variety. I have been looking for a tutorial or other
documentation detailing how to go about testing these plugins with
RSpec, but have not yet found anything helpful. Can anyone point me
toward such a thing?

Thanks,
Phillip
Posted by Matt Wynne (mattwynne)
on 2010-02-01 17:34
(Received via mailing list)
On 1 Feb 2010, at 15:14, Phillip Koebbe wrote:

> I have just recently started creating some plugins, mainly of the  
> controller variety. I have been looking for a tutorial or other  
> documentation detailing how to go about testing these plugins with  
> RSpec, but have not yet found anything helpful. Can anyone point me  
> toward such a thing?

Create a controller in your specs which uses the plugin in an
exemplary way, then asserts the behaviour of that controller.

cheers,
Matt

http://mattwynne.net
+447974 430184
Posted by Phillip Koebbe (pkoebbe)
on 2010-02-08 03:42
(Received via mailing list)
Matt Wynne wrote:
>
>

Thanks, Matt. I have some parts of it working now, but am stumped when
trying to call an action. I need to do a post :create, but post is not a
known method. Do you happen to know of a controller plugin that used
RSpec that I could peruse?

Thanks,
Phillip
Posted by Matt Wynne (mattwynne)
on 2010-02-08 10:14
(Received via mailing list)
On 8 Feb 2010, at 02:34, Phillip Koebbe wrote:

>>
>> Create a controller in your specs which uses the plugin in an  
>> exemplary way, then asserts the behaviour of that controller.
>>
>>
>
> Thanks, Matt. I have some parts of it working now, but am stumped  
> when trying to call an action. I need to do a post :create, but post  
> is not a known method. Do you happen to know of a controller plugin  
> that used RSpec that I could peruse?

Yeah, you need to convince RSpec that the describe blocks you're using
are describing an ExampleGroup that's about a Rails Controller, then
it will mix in the right methods for you. I think you can do something
like:

describe MySpecialTestController, :type => :controller do
end

That's the general idea. I think someone else on the list will be able
to help more than me with the specifics.

As for a plug-in, I don't know any off-hand... try a few popular ones
out on github and look for a spec directory in the root I guess.

>
> Thanks,
> Phillip
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

cheers,
Matt

http://mattwynne.net
+447974 430184
Posted by Phillip Koebbe (pkoebbe)
on 2010-02-08 17:03
(Received via mailing list)
Matt Wynne wrote:
> to help more than me with the specifics.
>
> As for a plug-in, I don't know any off-hand... try a few popular ones 
> out on github and look for a spec directory in the root I guess.
>

Thanks, again, Matt for taking the time to respond. I was kind of
surprised by how many plugins use test::unit. I did finally find
subdomain_fu [1] and was able to get something working. However, this
approach tests the controller in a project and requires rspec-rails to
be installed for the project. I would really like to be able to test
this independent of a project. So if anyone knows how to test a
controller plugin with RSpec independent of a project, I'd really
appreciate some pointers.

Here's what I have right now:

http://gist.github.com/298281

Thanks,
Phillip


[1] http://github.com/mbleigh/subdomain-fu
Posted by David Chelimsky (Guest)
on 2010-02-08 17:56
(Received via mailing list)
On Mon, Feb 8, 2010 at 11:58 AM, Phillip Koebbe 
<phillipkoebbe@gmail.com> wrote:
>> help more than me with the specifics.
> project. So if anyone knows how to test a controller plugin with RSpec
> independent of a project, I'd really appreciate some pointers.

The problem I've run into in trying to spec controller extensions in
isolation is that Rails controllers are not self-contained objects:
they need a bunch of surrounding state set up for them to work
properly. The testing facilities that ship with Rails hide that all
from you, but they do a lot of work for you in every test method, or
rspec code example.

In theory, you should be able to say:

=================================
require 'rubygems'
require 'action_controller/base'

class SomeController < ActionController::Base
  def index
    render :text => "this text"
  end
end

describe SomeController do
  describe "index" do
    it "returns some text" do
      c = SomeController.new
      c.index.should == "this text"
    end
  end
end
=================================

When you do, however, you get this:

  uninitialized constant ActionController::Metal (NameError)

Try to solve that and you'll be starting down a deep rabbit hole. And
even if you do solve that, the next rails release may well break
whatever you did to solve it. The safest bet is to spec your plugin in
the context of a complete rails app.

That said, I'd love to make this easier to do with rspec, but I won't
have cycles to drive this for quite some time. If anyone else is
interested in driving this, speak up and I'll be happy to assist.

Cheers,
David
Posted by Matt Wynne (mattwynne)
on 2010-02-08 18:20
(Received via mailing list)
On 8 Feb 2010, at 16:53, David Chelimsky wrote:

>>> describe MySpecialTestController, :type => :controller do
>>
>> independent of a project, I'd really appreciate some pointers.
> =================================
>  describe "index" do
>  uninitialized constant ActionController::Metal (NameError)
>
> Try to solve that and you'll be starting down a deep rabbit hole. And
> even if you do solve that, the next rails release may well break
> whatever you did to solve it. The safest bet is to spec your plugin in
> the context of a complete rails app.

So maybe we just need to build a library of helpers that make it easy
to create a modified rails app in Cucumber features?

>>
>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

cheers,
Matt

http://mattwynne.net
+447974 430184
Posted by Phillip Koebbe (pkoebbe)
on 2010-02-08 18:28
(Received via mailing list)
David Chelimsky wrote:
> require 'rubygems'
>      it "returns some text" do
>
> David
Well, there it is. :)

Thanks, David. I prefer to avoid rabbit holes.

Peace,
Phillip
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.