Example of REST controller specs? - ruby-on-rails

I'm trying to learn to do TDD. I'm having difficulty though, finding out what exactly it is I should be testing for each action in a RESTful controller. What I really would like is a sample of the most basic spec for a RESTful controller, which would help me figure out what I should be testing. If it matters, I'm using RSPec with Capybara. I'm not looking for something I can crib, just a sample I can examine so I know whether I'm going in the right direction.

There was an answer here before that got deleted, and the user suggested the Rails tutorial. It was pretty good for learning what to test, and can be found at http://www.railstutorial.org/book with chapters three and nine being most useful for someone just looking at it for TDD purposes.
Since then I've also found an older article that's really handy combined with the tutorial (and a bit of digging), simply because it's now highly out of date. This is found at the Everyday Rails blog, at http://everydayrails.com/2012/03/12/testing-series-intro.html.
Ultimately it looks like there may not be a good, solid, easy tutorial on how and what to test for Rails 4 with RSpec 3. Perhaps something you pros could consider!

Related

Need a basic simple tutorial on Rails with no tests, no scaffolding PLEASE

I understand models, views, and controllers from a design DRY perspective. How would I create a basic calculator in rails using best practices. Online there is way too much on scaffolding and tests that it just completely confuses the heck outa me. I dont know where to use what variables and how to decide where they go and all. I am a total noob. Please help.
For the basics, it's very useful read the rails guides.
If it is not enough you can view Ruby on Rails screencasts in railscasts

Rails: Refactoring restful controller specs: Why don't people do it?

Like most rails folks my restful controllers stick to a very consistent pattern, and any changes are rarely more than an extra line or two.
I like to test (using RSpec) fairly thoroughly, and that includes assigns, responds, redirects etc in controller tests.
The thing is, 75% of my controller specs follow the exact same pattern, and an obvious step seems to be to refactor them into a method/set of methods that I can call from each spec....either with a single (for example) 'restful_controller_specs' call, or individual 'restful_index_specs', 'restful_show_specs' etc for actions if the resource isn't completely standard.
And yet, when looking at other people's projects, from what I can see nobody else really seems to do that.
So in short, am I missing a good reason as to why not to agressively refactor restful controller specs?
I think most rails developers have learned programming by learning rails, which is not the ideal way to learn how to program, so things like refactoring are not a concept they have learned or encountered. Many people don't realise that files like config/routes.rb and db/migrate/* are just ruby code, and you can add loops and arrays and classes and subclasses into them as you need, just like any ruby program.
To all the rails coders out there: learn ruby! learn it well! It's a real programming language! Rails is made entirely of ruby.

Would Ruby on Rails suit my work..?

I wanted to make a web site with the following basic features- (1)User registration for buyers and sellers. (2)profile pages (3)A buyer should be able to post work and should get profile links of the corresponding seller who has expertise in that work.
As time progresses i would want to add more features to the site.The freelancer sites where user can post jobs and get bids is the best example of the work.
(1)I want my code to be maintainable as i woud be adding features later on. (2)It should be quick to develop. (3)Resources should be available(not the entire thing, atleast in bits and pieces) for the above requirements and should not be tough to find for future enhancements. (4)Design should be decoupled from the buisness logic as i would outsource the design work.
I was thinking of Ruby on Rails for this work as i have experience in the MVC model and RoR looks cool.I am from the mobility domain so i don't know whether RoR will suit my work
Would RoR suit this purpose.If yes where can i find the resources to the above mentioned requirements.
Thanks
Ruby on Rails would be ideal for this type of website.
Check out some of these resources for info on how to use Ruby on Rails:
http://guides.rubyonrails.org/
http://railscasts.com/
http://www.railsforum.com/
I noticed you are already aware of TeachMeToCode, but there is a tag there for all the Rails 3 tutorials, with some blog tutorials and what looks like the beginning of a series on how to build a del.icio.us clone. Since they are in Rails 3, they would be well worth checking out:
http://teachmetocode.com/screencasts/tag/rails-3/
One of the best tutorials:
http://railsforzombies.org
It will let you have your own point of view quickly.
It depends on with which languages you have experience. Any good MVC framework will do the job just fine but if you like Ruby syntax RoR is definitely a good framework to develop this kind of application.

How to document a rails application?

I just started to document a rails application. I know this is actually done by rdoc, so I followed some rdoc guides regarding syntax and so on, but I got stuck when I tried to describe attributes of models, validations and the relationship between models, mostly because these things are part of ActiveRecord. So I wonder if there is some guide or a good practice regarding how to document a rails application or if there is something I'm missing?
I know that I could put all of this in the class description, but I wonder if there is a way more closely tied to the declaration itself (has_many, validates_presence_of, etc.) and what about the attributes?
I personally prefer YARD - http://yardoc.org , as it does a better job in documenting IMHO.
I don't know if there's a specific handler for Rails available, but it's quite easy to write one - http://yardoc.org/guides/extending-yard/writing-handlers.html
A good example might be the attribute handler - part of the yard gem:
lib/yard/handlers/ruby/attribute_handler.rb
Remember your tests are part of the documentation (for developers), particularly if you are using Cucumber where scenarios are easy to read. If you keep your methods very short and there is a test method with a descriptive name e.g. "should set the users name" I find I typically don't need comments on the method.
Validations or other parts of Rails I would not document. Part of being a Rails developer is understanding how these work, I think it is a fair assumption that another maintainer of your code reading it down the road will know validations, or other things built in to Rails. By that same logic, if you can use features of the framework or happy paths (not deviate much) with [documented] third party code, a lot of the documentation will be written for you.

Concept of, Framework for , How to Test View of MVC (Ruby on Rails)

I am now doing the UI from fresh so I want to make it fully test.
Could anyone suggest the correct way to do this and where should be the good place to start?
such as Concept, Framework.
(I already know some concept of testing)
Maybe Selenium can help you with frontend testing.
Take a look at The RSpec Book. The book goes over much more than just using RSpec (the testing framework). It also delves into the methodology of BDD (Behavior-Driven Development) starting from testing the UI using a tool like Cucumber all the way down through the rest of the Rails stack. It's probably the best book on the entire subject for practical testing in Rails right now. Most of the concepts in the book will apply to other languages and technologies as well
Try the Cucumber site. The site has a bunch of documentation and videos to get you started.
Watir may be useful as a tool and there are various tutorials out there for it.

Resources