[LRUG] Cucumber and RSpec on Rails

Matthew Willson matthew at playlouder.com
Tue Feb 9 03:48:57 PST 2010


I'm also developing a thick-client app (we're using dojo for the client, but similar idea) behind a ruby REST API.

Currently we have API-specific tests based on rack/test (very handy library, based on merb's controller test helpers). I've found because it's an API-based app we have less logic in front of the model, so this kind of request-response testing approach pins down errors quite precisely without the need for so much in the way of model-specific unit tests.

But they're still quite fine-grained when it comes to integration with the client UI; investigating Selenium for testing the application UI itself, although maybe something like Cucumber+Watir would be an easier choice. Also interested to hear what people are using.


As an aside - got quite frustrated with Rails' (and Merbs') slightly-confused implementation of concepts from REST when implementing a serious REST-based API application; We ended up moving to a Rack-based approach which I wrapped up as a gem:

http://github.com/mjwillson/doze

Still a bit rough and ready but the idea is that the key abstractions in REST-based development should be things like Resource, Media type and Entity, rather than Model and Controller.

So you end up with a more componentized Resource model, which the framework knows how to expose to the web via HTTP, rather than a tiered approach with enforced separation between routing, controller and model layers. This makes routing and controller logic re-usable alongside the resource model, and aims to remove (or at least, tidy up and move out of view) a lot of the boilerplate one finds in Rails API controllers.

Comparisons would be things like
Erlang's webmachine: http://bitbucket.org/justin/webmachine/wiki/Home ("Webmachine is not much like the Web frameworks you're used to. You can call Webmachine a REST toolkit if you like")
Or RESTLet: http://www.restlet.org/

Can elaborate further if anyone's interested in this kind of approach

-Matt

On 9 Feb 2010, at 11:10, Steve Graham wrote:

> My usual approach is to test the model layer with RSpec, and write Cucumber stories for integration testing. Cucumber does a good job of touching most of the controller layer, and of course one can write specs for the rest.
> 
> I want to get the opinion of anyone who cares to chip in on this: At work we're developing a Sproutcore app with a Rails backend. As the Sproutcore app expects JSON in a certain structure, how and with what would you test this? I'm thinking writing controller specs, but does the Cucumber story paradigm make more (or any) sense for this use case. Personally I don't think it does, but I'm interested to hear your arguments for and against.
> 
> Finally has anyone on here developed a Sproutcore app? If so, what did you use for integration testing? I'd like to use Cucumber for this but with what? Webrat? Capybara? Watir?
> 
> S
> 
> 
> On 9 Feb 2010, at 09:43, chat-request at lists.lrug.org wrote:
> 
>> Send Chat mailing list submissions to
>> 	chat at lists.lrug.org
>> 
>> To subscribe or unsubscribe via the World Wide Web, visit
>> 	http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>> or, via email, send a message with subject or body 'help' to
>> 	chat-request at lists.lrug.org
>> 
>> You can reach the person managing the list at
>> 	chat-owner at lists.lrug.org
>> 
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of Chat digest..."
>> 
>> 
>> Today's Topics:
>> 
>>  1. Re: Cucumber and RSpec on Rails (Jordi Noguera Leon)
>>  2. Re: Cucumber and RSpec on Rails (Joseph Wilk)
>> 
>> 
>> ----------------------------------------------------------------------
>> 
>> Message: 1
>> Date: Tue, 9 Feb 2010 00:39:05 +0000
>> From: Jordi Noguera Leon <jordinoguera83 at gmail.com>
>> To: London Ruby Users Group <chat at lists.lrug.org>
>> Subject: Re: [LRUG] Cucumber and RSpec on Rails
>> Message-ID:
>> 	<f8eaf1df1002081639g61934975k793e02c9e249278b at mail.gmail.com>
>> Content-Type: text/plain; charset="utf-8"
>> 
>> Thanks for your answers guys.
>> 
>> @Murray, that was a nice vid. Apparently, a rule of thumb would be to code
>> in the specs the stuff you wouldn't (or couldn't) talk with the business
>> guys and in the features the stuff you would discuss with them...
>> 
>> 
>> 2010/2/8 Murray Steele <murray.steele at gmail.com>
>> 
>>> Joseph Wilk's talk at Scotland On Rails last year is a *really* good intro
>>> into how features and tests (both controller and model) sit together
>>> properly.  I'd highly recommend watch it to get a grip on how you should
>>> approach things, although if you aren't into watching videos, Neil has
>>> pretty much nailed it (although he doesn't mention model tests).
>>> 
>>> Watch it here:
>>> http://scotland-on-rails.s3.amazonaws.com/1A06_JosephWilk-SOR.mp4
>>> 
>>> One thing to note is that in most Railscasts Ryan Bates isn't showing you
>>> the golden path of the true developer, just how to use the gem or library
>>> he's covering in that episode.  Accordingly he hardly ever writes tests,
>>> unless it's a testing library or gem he's showing off.  So it's not
>>> surprising that in the cucumber episodes he doesn't do unit tests, as, in
>>> theory, you already know how to write those, what you want to learn is how
>>> to write cucumber features.
>>> 
>>> Muz
>>> 
>>> 
>>> On 8 February 2010 22:12, Neil Edwards <neil at plasticwater.com> wrote:
>>> 
>>>> Agreed, although it's sometimes still nice to hit the controller steps.
>>>> If you get autospec working with cucumber it's very nice to follow
>>>> something like:
>>>> 
>>>> write up your cucumber feature
>>>> watch it fail
>>>> write up your rspec controller test
>>>> watch it fail
>>>> make your rspec test pass
>>>> make your cucumber test pass
>>>> 
>>>> autospec won't rerun the cucumber tests until the rspec ones are passing,
>>>> it's quite clever like that.
>>>> 
>>>> I'd say most of my controllers don't have test. The integration testing
>>>> that cucumber provides is really nice and done correctly can give you
>>>> massive amounts of confidence your code works like you want. I just like to
>>>> have some controller tests in there every now and again for things that I
>>>> think are a little bit too complex.
>>>> 
>>>> Check out the rspec book too from pragprog.com, I quite liked it and has
>>>> lots of cucumber in it too.
>>>> 
>>>> On 8 February 2010 22:02, <chat-request at lists.lrug.org> wrote:
>>>> 
>>>>> Send Chat mailing list submissions to
>>>>>      chat at lists.lrug.org
>>>>> 
>>>>> To subscribe or unsubscribe via the World Wide Web, visit
>>>>> 
>>>>>      http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>>>> or, via email, send a message with subject or body 'help' to
>>>>>      chat-request at lists.lrug.org
>>>>> 
>>>>> You can reach the person managing the list at
>>>>>      chat-owner at lists.lrug.org
>>>>> 
>>>>> When replying, please edit your Subject line so it is more specific
>>>>> than "Re: Contents of Chat digest..."
>>>>> 
>>>>> Today's Topics:
>>>>> 
>>>>> 1. Cucumber and RSpec on Rails (Jordi Noguera Leon)
>>>>> 2. Another Recruitment Shout-Out (Hakan ?enol Ensari)
>>>>> 3. Re: Cucumber and RSpec on Rails (Paul Campbell)
>>>>> 4. Re: Cucumber and RSpec on Rails (Steven Mohapi-Banks)
>>>>> 
>>>>> 
>>>>> 
>>>>> ---------- Forwarded message ----------
>>>>> From: Jordi Noguera Leon <jordinoguera83 at gmail.com>
>>>>> To: London Ruby Users Group <chat at lists.lrug.org>
>>>>> Date: Mon, 8 Feb 2010 19:42:48 +0000
>>>>> Subject: [LRUG] Cucumber and RSpec on Rails
>>>>> Hi there!
>>>>> 
>>>>> I'm starting with Cucumber but I'm not sure if/when/how to create specs.
>>>>> In the couple of Railscasts I'seen so far, the guy generates rspec_models
>>>>> and rspec_controllers, but he doesn't code anything inside them... In
>>>>> another screencast I've seen, the guy validates uniqueness of some stuff
>>>>> inside the spec, but I guess that's something you could do using a
>>>>> scenario, right? Basically, do I need to write any spec or I can do all the
>>>>> stuff in the scenario?
>>>>> 
>>>>> Regards,
>>>>> Jordi
>>>>> 
>>>>> 
>>>>> ---------- Forwarded message ----------
>>>>> From: "Hakan ?enol Ensari" <hakan.ensari at papercavalier.com>
>>>>> To: London Ruby Users Group <chat at lists.lrug.org>
>>>>> Date: Mon, 8 Feb 2010 19:51:16 +0000
>>>>> Subject: [LRUG] Another Recruitment Shout-Out
>>>>> Hi everyone,
>>>>> 
>>>>> My apologies for spamming.
>>>>> 
>>>>> My company is looking for a full-time Ruby dev in London. Here are the
>>>>> details:
>>>>> 
>>>>> http://papercavalier.com/jobs
>>>>> 
>>>>> Cheers,
>>>>> Hakan
>>>>> 
>>>>> http://ultra.bohe.me
>>>>> 
>>>>> 
>>>>> ---------- Forwarded message ----------
>>>>> From: Paul Campbell <paul at rslw.com>
>>>>> To: London Ruby Users Group <chat at lists.lrug.org>
>>>>> Date: Mon, 8 Feb 2010 19:57:37 +0000
>>>>> Subject: Re: [LRUG] Cucumber and RSpec on Rails
>>>>> Jordi,
>>>>> 
>>>>> cucumber can (and will, if you want) cover 90% of the cases.
>>>>> 
>>>>> Sometimes you might want to drop down to controller testing for edge
>>>>> cases or redirects to  external services and things.
>>>>> 
>>>>> Mostly though, my controller specs remain empty.
>>>>> 
>>>>> Model specs are much more frequent though, for testing things like
>>>>> arithmetic, maybe validations, auto-assignment, data transformations
>>>>> etc.
>>>>> 
>>>>> Hope this helps,
>>>>> 
>>>>> ?Paul
>>>>> 
>>>>> On Mon, Feb 8, 2010 at 7:42 PM, Jordi Noguera Leon
>>>>> <jordinoguera83 at gmail.com> wrote:
>>>>>> Hi there!
>>>>>> I'm starting with Cucumber but I'm not sure if/when/how to create
>>>>> specs. In
>>>>>> the couple of Railscasts I'seen so far, the guy generates rspec_models
>>>>> and
>>>>>> rspec_controllers, but he doesn't code anything inside them... In
>>>>> another
>>>>>> screencast I've seen, the guy validates uniqueness of some stuff inside
>>>>> the
>>>>>> spec, but I guess that's something you could do using a  scenario,
>>>>> right?
>>>>>> Basically, do I need to write any spec or I can do all the stuff in the
>>>>>> scenario?
>>>>>> Regards,
>>>>>> Jordi
>>>>>> _______________________________________________
>>>>>> Chat mailing list
>>>>>> Chat at lists.lrug.org
>>>>>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>>>>> 
>>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>> --
>>>>> 
>>>>> 
>>>>> Paul Campbell
>>>>> paul at rushedsunlight.com
>>>>> - - - - - - - - - - - - - - - - - - -
>>>>> blog http://www.pabcas.com
>>>>> twitter http://www.twitter.com/paulca
>>>>> github http://www.github.com/paulca
>>>>> phone +353 87 914 8162
>>>>> - - - - - - - - - - - - - - - - - - -
>>>>> 
>>>>> 
>>>>> 
>>>>> ---------- Forwarded message ----------
>>>>> From: Steven Mohapi-Banks <steven.mohapibanks at me.com>
>>>>> To: London Ruby Users Group <chat at lists.lrug.org>
>>>>> Date: Mon, 08 Feb 2010 21:40:35 +0000
>>>>> Subject: Re: [LRUG] Cucumber and RSpec on Rails
>>>>> I'd pretty much agree with this - my controller specs these days are
>>>>> pretty much for covering access privileges, security edge cases and the
>>>>> like. I also spec out routing rules quite frequently. Everything else goes
>>>>> in Cucumber stories.
>>>>> 
>>>>> Steve
>>>>> 
>>>>> 
>>>>> On Monday, February 08, 2010, at 07:57PM, "Paul Campbell" <paul at rslw.com>
>>>>> wrote:
>>>>>> Jordi,
>>>>>> 
>>>>>> cucumber can (and will, if you want) cover 90% of the cases.
>>>>>> 
>>>>>> Sometimes you might want to drop down to controller testing for edge
>>>>>> cases or redirects to  external services and things.
>>>>>> 
>>>>>> Mostly though, my controller specs remain empty.
>>>>>> 
>>>>>> Model specs are much more frequent though, for testing things like
>>>>>> arithmetic, maybe validations, auto-assignment, data transformations
>>>>>> etc.
>>>>>> 
>>>>>> Hope this helps,
>>>>>> 
>>>>>> ?Paul
>>>>>> 
>>>>>> On Mon, Feb 8, 2010 at 7:42 PM, Jordi Noguera Leon
>>>>>> <jordinoguera83 at gmail.com> wrote:
>>>>>>> Hi there!
>>>>>>> I'm starting with Cucumber but I'm not sure if/when/how to create
>>>>> specs. In
>>>>>>> the couple of Railscasts I'seen so far, the guy generates rspec_models
>>>>> and
>>>>>>> rspec_controllers, but he doesn't code anything inside them... In
>>>>> another
>>>>>>> screencast I've seen, the guy validates uniqueness of some stuff
>>>>> inside the
>>>>>>> spec, but I guess that's something you could do using a  scenario,
>>>>> right?
>>>>>>> Basically, do I need to write any spec or I can do all the stuff in
>>>>> the
>>>>>>> scenario?
>>>>>>> Regards,
>>>>>>> Jordi
>>>>>>> _______________________________________________
>>>>>>> Chat mailing list
>>>>>>> Chat at lists.lrug.org
>>>>>>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>>>>>> 
>>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> --
>>>>>> 
>>>>>> 
>>>>>> Paul Campbell
>>>>>> paul at rushedsunlight.com
>>>>>> - - - - - - - - - - - - - - - - - - -
>>>>>> blog http://www.pabcas.com
>>>>>> twitter http://www.twitter.com/paulca
>>>>>> github http://www.github.com/paulca
>>>>>> phone +353 87 914 8162
>>>>>> - - - - - - - - - - - - - - - - - - -
>>>>>> _______________________________________________
>>>>>> Chat mailing list
>>>>>> Chat at lists.lrug.org
>>>>>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>>>>> 
>>>>>> 
>>>>> 
>>>>> 
>>>>> _______________________________________________
>>>>> Chat mailing list
>>>>> Chat at lists.lrug.org
>>>>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>>>> 
>>>>> 
>>>> 
>>>> _______________________________________________
>>>> Chat mailing list
>>>> Chat at lists.lrug.org
>>>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>>> 
>>>> 
>>> 
>>> _______________________________________________
>>> Chat mailing list
>>> Chat at lists.lrug.org
>>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>> 
>>> 
>> -------------- next part --------------
>> An HTML attachment was scrubbed...
>> URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20100209/ba2ef59c/attachment-0001.htm>
>> 
>> ------------------------------
>> 
>> Message: 2
>> Date: Tue, 09 Feb 2010 09:42:55 +0000
>> From: Joseph Wilk <joe at josephwilk.net>
>> To: chat at lists.lrug.org
>> Subject: Re: [LRUG] Cucumber and RSpec on Rails
>> Message-ID: <4B712E1F.1050904 at josephwilk.net>
>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>> 
>> On 08/02/2010 19:42, Jordi Noguera Leon wrote:
>>> Hi there!
>>> 
>>> I'm starting with Cucumber but I'm not sure if/when/how to create 
>> specs. In the couple of Railscasts I'seen so far, the guy generates 
>> rspec_models and rspec_controllers, but he doesn't code anything inside 
>> them... In another screencast I've seen, the guy validates uniqueness of 
>> some stuff inside the spec, but I guess that's something you could do 
>> using a  scenario, right? Basically, do I need to write any spec or I 
>> can do all the stuff in the scenario?
>> 
>> Hello,
>> 
>> While there is not a fit all rule, it comes down to considering a couple 
>> of points:
>> 
>> 1. Complexity/Distance/Focus
>> The scenario focuses on the real value realised by the system (the 
>> output), there can often be quite a distance between the implementation 
>> and the output. Hence you're failing feature might leave you very 
>> stumped as to what is wrong/what to do next. You may also have complex 
>> logic that you feel like needs more developer focused tests and feedback 
>> (a tighter feedback loop). In these cases specs can help you out.
>> 
>> 2. Speed
>> In Cucumber we try and exercise the entire application stack and avoid 
>> mocking where possible. As a consequence things can get slow. If running 
>> a cucumber scenario takes 4/5 minutes that could significantly slow down 
>> your development cycle (While it still as a test has a lot of value 
>> testing everything interacts nicely). Hence you might drop down to a 
>> spec and stub out an area of the system to help achieve faster feedback 
>> (not that is the only reason to stub, it all helps achieve focus as we 
>> can isolate from the other systems we don't care about.)
>> 
>> 3. Fragility
>> In my experience Features/Cucumber tend to live longer than specs. When 
>> I have performed major refactoring some of the specs tended to be thrown 
>> away while the features stuck around (the step defs may however have 
>> changed). That does not mean the specs weren't useful, just something to 
>> think about.
>> 
>> 4. Personal taste
>> Some people Just Cuke, some people just spec and some people do both.
>> For me personally I use both, that does not mean its the right way for 
>> you. Play around and get a feel for how you want to use these tools.
>> 
>> HTH
>> --
>> Joseph Wilk
>> http://blog.josephwilk.net
>> +44 (0)7812 816431
>> 
>> 
>>> 
>>> Regards,
>>> Jordi
>>> 
>>> 
>>> _______________________________________________
>>> Chat mailing list
>>> Chat at lists.lrug.org
>>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>> 
>> 
>> 
>> 
>> ------------------------------
>> 
>> _______________________________________________
>> Chat mailing list
>> Chat at lists.lrug.org
>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>> 
>> 
>> End of Chat Digest, Vol 49, Issue 11
>> ************************************
> 
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org




More information about the Chat mailing list