Test Driven Development (TDD) with Rails - ruby-on-rails

I am looking for TDD resources that are specific to Rails.
I've seen the Rails Guide: The Basics of Creating a Rails Plugin which really spurred my interest in the topic.
I have the Agile Development with Rails book and I see there's some testing-related information there. However, it seems like the author takes you through the steps of building the app, then adds testing afterward. This isn't really Test Driven Development.
Ideally, I'd like a book on this, but a collection of other tutorials or articles would be great if such a book doesn't exist.
Things I'd like to learn:
Primary goal: Best Practices
Unit testing
How to utilize Fixtures
Possibly using existing development data in place of fixtures
What's the community standard here?
Writing tests for plugins
Testing with session data
User is logged in
User can access URL /foo/bar
Testing changes in data
Author updates post
Product status changes to back order
User buys product (behavior?)
User is logged in
User has valid address and valid credit card
Order record is made
Credit Card transaction history updated
Send email to user
Testing success of sending email
Testing AJAX
Possibly testing general jQuery functionality (Interface elements, etc)
RSpec
What place does RSpec have in a Rails app
The future of testing
Where is testing going? What things are on their way out?
What patterns will be used in Rails 3?
Thanks for any help!

The RSpec Book: Behaviour Driven Development with RSpec, Cucumber, and Friends. Focuses on BDD rather than TDD, but that's the whole point of RSpec and Cucumber. Not entirely specific to Rails, but has several chapters dedicated to it. Well written and up to date (though still in beta).

Many of the points you mention are discussed in different episodes of Railscasts by Ryan Bates, including:
http://railscasts.com/episodes/275-how-i-test
http://railscasts.com/episodes/158-factories-not-fixtures-revised
http://railscasts.com/episodes?utf8=%E2%9C%93&search=cucumber

This is an older question, but I will add that Michael Hartl's Learn Rails by Example walks the user through TDD and covers most of the bullet points mentioned in the original question.
I'll also add that Everyday Rails Testing with RSpec is a great purchase, especially since the author makes free updates available to customers after they already bought the book, and he plans on updating again in light of Rails 4's release.

Related

Ruby_on_Rails Testing The Database Using Rspec

Actually i am newbie to rails testing
I want to know what are all the step covered in the rails testing.?
Could anyone help me knowing the steps involve in Professional Testing
Check out the rails guide Gavin linked you. Can also check out this book: http://pragprog.com/book/achbd/the-rspec-book. It covers how to use rspec, and covers testing various parts of the application.
I also rather enjoyed Rails 3 in action, which teaches the basics and various topics of rails, while doing inside-out testing throughout the entire book. Rails 4 in action is a work in progress edition as well.
As for my answer:
Generally testing first before you write code makes the process better & easier. Writing tests after the fact isn't as nice, even though it does give you the coverage. When it's part of your regular workflow to add features, it's quite nice. If you have a new feature that you want to add, such as user sign up that sends a confirmation email. You can write the integration test first. It describes the user as going through the sign up form, filling it properly, and then checking if an email was sent.
From there you can test internals of the user model, making sure methods you build for this to work function as expected. Then when you have your tests green, add another scenario to the integration tests where the login was invalid.

How to create Rails App using cucumber

I am new for cucumber. I want to create on sample application using cucumber.
I am working on Windows 7 OS. Can any one guide me?
Thanks.
I think these are two separate problems:
Building a Rails application on Windows
Building a Rails application with Cucumber (using principles promoted by Cucumber like BDD, Specification by Example and an outside-in workflow)
Building a Rails application on Windows
There are a lot of tutorials, best starting point is probably: Getting Started with Rails on Rails Guides.
However the official advice there is using a virtual machine with Linux :-)
Here is a good thread with advice for running Rails under Windows.
Building a Rails application with Cucumber
An extraordinary example for this is the Book Rails 3 in Action by Manning.
This book really excels at showing how to create an application outside-in with Specification by Example using Cucumber.
In each chapter of the book a new feature is implemented. There are almost no screenshots explaining those features. Instead the authors are using Cucumber scenarios at the beginning of each chapter to exactly specify the desired new behavior that is the topic of given chapter. Then they are showing and explaining the necessary steps and the code that is needed to implement the functionality.
This is the best example I have found for using Cucumber with Rails so far.
This is also a really great example how Specfication by Example can be used for building a shared understanding - since teaching is all about sharing understanding.
However the scenarios in the book are quite focusing of specifying behavior on the UI level ... which is not commonly agreed on being best practice. But for teaching Rails it is great.
Rails 3 in Action is still in the early access program. I don't know where the official sources are.
But when you search on GitHub you already find different implementations of the example application (Tiketee):
https://github.com/rubykr/ticketee
https://github.com/oMartell/ticketee-rails-3-in-action
https://github.com/rails3book/ticketee
Here is the guide how to do it.
I found an interesting webcast video that does just this.
The basic procedure is define a couple features in cucumber. (actually the gherkin language)
Run cucumber
Fix errors
Repeat the
2 steps until you have a
working app.
It makes use of a few RoR gems like webrat and rspec.
http://railscasts.com/episodes/155-beginning-with-cucumber

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.

Process for beginning a Ruby on Rails project

I'm about to begin a Ruby on Rails project and I'd love to hear how others go through the process of starting an application design. I have quite a bit of experience with RoR, but don't have that many starting from scratch with only a vision experiences and would appreciate the wisdom of others who've been there.
I'm looking for an order of events, reasons for the order, and maybe why each part is important. I can think of a few starting points, but I'm not sure where it's best to begin
Model design and relationships (entities, how they relate, and their attributes)
Think of user use-cases (or story-boards) and implement the minimum to get these done
Create Model unit-tests then create the necessary migrations and AR models to get the tests to pass
Hack out the most basic version of the simplest part of your application and go from there
Start with a template for a rails app (like http://github.com/thoughtbot/suspenders)
Do the boring gruntwork first (User auth, session management, ...)
...
I found myself looping the following tasks for most projects:
Gather User requirements
Design database Models
Build Views
Streamline Layouts
Find and learn Plug-ins/Gems
Testing
User Review/Acceptance
Deploy the app
Documentation
After these years of working as a freelancer, I think step 1 and 2 are the most important (at least for small projects). Before writing any code, I urge users to finalize all UI first. HTML skeleton is better than written document. Users do not and will not understand software specifications. They can only give feedback after they see can click something. So being fluent in building a HTML site is a helpful skill. Sometimes I delegate the task to a partner, as a SA role.
Rails is very good for building an ever-evolving schema. Try to use migrations and data seeding instead of writing SQL statements directly. I find myself rely on ActiveRecord more and more overtime. The script/console is a nice tool to test out those many-to-many relationships and building :conditions =>.
I worked on a few legacy database lately, the establish_connection and set_table_name feature in ActiveRecord glue old and new database elegantly.
I'd also like to use this chance to thank Ryan Bates, I learn a lot of Rails from his railscasts.

Rails - Any plugins for building a online quiz/test?

I'm trying to add some online tests to my site and before I rolled up my sleeves I was wondering if there is any ruby on rails plugin support for such a task.
Essentially I'm looking for something that lets me add multiple choice questions. Performing the scoring logic would be an added bonus.
Any one know of anything?
Ryan Bates has a screencast on nested model forms where the example app is a survey tool You could use it as a starting point.

Resources