Rspec controller testing

0 views
Skip to first unread message

Carl Woodward

unread,
Aug 28, 2008, 9:02:06 PM8/28/08
to rails-...@googlegroups.com
Hi all,

Just wondering if anyone knows any plugins that make rspec controller
testing more concise rather then repeating heaps of code for every
action?

Would love to hear some thoughts on controller and view testing and
what everyone tests out there.

Thanks,
Carl.

--
Carl Woodward
0412218979
cjwoo...@gmail.com

Lachie

unread,
Aug 28, 2008, 9:07:20 PM8/28/08
to rails-...@googlegroups.com
On Fri, Aug 29, 2008 at 11:02 AM, Carl Woodward <cjwoo...@gmail.com> wrote:
>
> Hi all,
>
> Just wondering if anyone knows any plugins that make rspec controller
> testing more concise rather then repeating heaps of code for every
> action?

http://blog.davidchelimsky.net/2008/7/1/new-controller-examples

>
> Would love to hear some thoughts on controller and view testing and
> what everyone tests out there.
>
> Thanks,
> Carl.
>
> --
> Carl Woodward
> 0412218979
> cjwoo...@gmail.com
>
> >
>

--
:lachie
http://smartbomb.com.au
http://www.flickr.com/photos/lachie/

Carl Woodward

unread,
Aug 28, 2008, 9:10:06 PM8/28/08
to rails-...@googlegroups.com
Hi Lachie,

Thats the way that I am writing them. I feel like I am writing nice
tight controller code and 3 times the test code, but not because its
tested in multiple ways or scenarios but because the test code is
longer.

Any thoughts?

Thanks,
Carl.

Lachie

unread,
Aug 28, 2008, 9:14:06 PM8/28/08
to rails-...@googlegroups.com
On Fri, Aug 29, 2008 at 11:10 AM, Carl Woodward <cjwoo...@gmail.com> wrote:
>
> Hi Lachie,
>
> Thats the way that I am writing them. I feel like I am writing nice
> tight controller code and 3 times the test code, but not because its
> tested in multiple ways or scenarios but because the test code is
> longer.
>
> Any thoughts?

Well David's point (if i understand it) is that writing a bit more
clearly will make your specs less brittle when things change

Lachie

unread,
Aug 28, 2008, 9:15:04 PM8/28/08
to rails-...@googlegroups.com
On Fri, Aug 29, 2008 at 11:10 AM, Carl Woodward <cjwoo...@gmail.com> wrote:
>
> Hi Lachie,
>
> Thats the way that I am writing them. I feel like I am writing nice
> tight controller code and 3 times the test code, but not because its
> tested in multiple ways or scenarios but because the test code is
> longer.

you could also try rr for much terser mock syntax:
http://github.com/btakita/rr/tree/master

Carl Woodward

unread,
Aug 28, 2008, 9:19:58 PM8/28/08
to rails-...@googlegroups.com
Why couldn't:

it "should be successful" do
response.should be_success
end

be

it_should_be_successful

and

it "should render edit template" do
response.should render_template('edit')
end

be

it_should_render_edit_template

I'm just looking at getting rid of lines.

I'm trying to implement this now. Would love to know peoples throughts.

Ideally you would then have a couple of lines of it should do response stuff
and then your mocking functionality. Maybe I'm being silly. I do like typing...

Xavier Shay

unread,
Aug 28, 2008, 9:42:11 PM8/28/08
to rails-...@googlegroups.com

Enrico Teotti

unread,
Aug 29, 2008, 1:21:04 AM8/29/08
to rails-...@googlegroups.com
Carl did you try webrat?
http://www.brynary.com/2007/12/8/webrat-0-1-0-released
the same dude did a nice talk:
http://goruco2008.confreaks.com/01_helmkamp.html


2008/8/29 Xavier Shay <xavie...@rhnh.net>:

--
Enrico Teotti
IT consultant, accessible web sites and web applications
Sydney, NSW, Australia
enrico...@gmail.com
mobile (IT) +393286590765
mobile (AU) +00610416748450

http://www.teotti.com

George

unread,
Aug 29, 2008, 1:55:02 AM8/29/08
to rails-...@googlegroups.com
On Fri, Aug 29, 2008 at 11:19 AM, Carl Woodward <cjwoo...@gmail.com> wrote:

Why couldn't:

it "should be successful" do
   response.should be_success
end

be

it_should_be_successful

and

it "should render edit template" do
   response.should render_template('edit')
end

be

it_should_render_edit_template

Hi Carl,

If you're willing to step out of rspec, shoulda provides exactly this sort of thing for test/unit:

  http://github.com/thoughtbot/shoulda/tree/master

It also lets you nest test cases, which can further reduce repetition.

George.

Adam Salter

unread,
Aug 29, 2008, 4:02:51 AM8/29/08
to rails-...@googlegroups.com
Carl,
My response would be/is to use:
http://jamesgolick.com/2007/10/19/introducing-resource_controller-focus-on-what-makes-your-controller-special

and basically only write tests for what "makes your controller
special"... It's nobody's fault, but controllers are currently _not_
DRY IMHO.

Best,
-Adam

Adam Salter

unread,
Aug 29, 2008, 4:06:45 AM8/29/08
to rails-...@googlegroups.com
Carl,
I personally don't like this style.

Rails has just gone the reverse direction with Test::Unit.

http://github.com/rails/rails/commit/f74ba37f4e4175d5a1b31da59d161b0020b58e94

-Adam

Mike Williams

unread,
Aug 29, 2008, 11:59:24 PM8/29/08
to rails-...@googlegroups.com
On 29/08/2008, at 11:19 AM, Carl Woodward wrote:

Why couldn't:

it "should be successful" do
   response.should be_success
end

be

it_should_be_successful

It could. I've certainly creating syntax sugar like that for specs.  You'd put your spec-sugar methods in a shared spec:

describe "a typical controller", :shared => true do

  def self.it_should_be_successful

    it "should be successful" do
      response.should be_success
    end
  end

end

and then include and use them 

describe "MyController" do

  it_should_behave_like "a typical controller"

  it_should_be_successful

end

Although: test-generator methods like this are most useful when they take parameters.  In cases like yours, a simply shared spec might be sufficient:

describe "a typical controller", :shared => true do


  it "should be successful" do
    response.should be_success
  end

end

describe "MyController" do

  it_should_behave_like "a typical controller"

end

I hope that helps.

-- 
cheers, 
Mike Williams



Reply all
Reply to author
Forward
0 new messages