writing test for rubyonrails - ruby-on-rails

I am new to writing test for rubyonrails app like rspect, steak, cucumber etc. How do I start or learn so that it will help me to write beautiful codes?
You can give some simple example or some url where I can understand from zero.
Thanks

http://railscasts.com/episodes/155-beginning-with-cucumber - a good podcast for beginner
The RSpec Book - also very useful
It's a good point to start and then Google help you

1) Start by reading the rails testing guide just to get a feel for testing.
2) Become familiar with the various testing frameworks (Cucumber, rspec, testunit, steak, minitest), watch screencasts (railscasts, TeachMeToCode) and then try and choose one that appeals to you.
3) Write tests!! and get help as you need it from StackOverflow. If you like RSpec then the RSpec book is very helpful, but not essential.
My personal opinion on frameworks:
I started out really liking cucumber but I eventually found it very verbose and I didn't like using it for my integration tests and then RSpec for my unit and functional tests. I now like using just RSpec with Capybara and Shoulda matchers. TestUnit can be even less verbose than RSpec but personally I find RSpec to be the happy medium between writing self documenting code and not being overly verbose.

I've just started with RSpec a few days ago and it was pretty hard (it still is actually). You could start from railstutorial.org. The author follows the TDD principle. More info available at RSpec and Cucumber github page. Be sure to check out the wiki.

Related

Example of REST controller specs?

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!

How to write cucumber test cases?

Can anyone tell me how to write cucumber testcases as am new to this.Suggest me best tutorial togo through for this.
Thanks in Advance.
Rails cast is nice
Nice book for basic setup: The Cucumber Book: Behaviour-Driven Development for Developers and Testers
Also, the Nettuts+ tutorial: Ruby for Newbies: Testing Web Apps with Capybara and Cucumber
This screencast is the best out there. Do checkout part 2 as well! BTW, in cucumber parlance, they are called 'features' where you describe scenarios.
I can personally recommend "The RSpec Book: Behaviour-Driven Development with RSpec, Cucumber, and Friends" if you want to know more about BDD with Cucumber although I am in no way related to the authour(s) and/or the publisher.
Assuming that you have setup cucumber, I would recommend to use https://github.com/cucumber/cucumber/wiki/Scenario-Outlines

Cucumber and Rspec

Can anyone suggest me good source (Easy examples) for cucumber and rspec tutorials (rails 3)???
Edit:
Actually I am looking for free online resources with good examples..
I think the RSpec Book is an excellent resource on Cucumber, RSpec and BDD.
Agree - the RSpec Book provides a wealth of information. I found getting the flow was a big problem in the learning. The book gives you the quick intro, then dives into details and by the time you surface your head will be swimming. It takes a couple of goes of reading the book to get the finer points.
Another great read is a blog post by Sarah Mei called Outside-In BDD. This dialog really help to make things gel since it's demonstrating the application of Cucumber/Rspec in the context of the flow a developer would go through.
Then there is the classics such as the Railscast from Ryan Bates. There are a couple of episodes on Cucumber and other tools and the snippets give you a nice overview after you've soaked up the RSpec book.
For Rspec you can read the Michael Hartl book "Rails 3 tutorial" there is a lot of testing examples : Here the online book
For cucumber, you can watch teachmetocode screencasts (website) . There are good examples using cucumber when building an application from scratch.
Hope it could help!
Screencast walk-throughs can be good sources to grok them
Peepcode has a few, like this one on Cucumber, which has been updated this year - so I'm assuming it's good for rails 3

How do you develop outside-in Rails app using Cucumber & RSpec?

I just get started using BDD in Rails application, but I'm not sure what are best practices and workflows? And what other things that I really need for testing for my project such as step definitions, controllers, models, and views? Do I need to test all of those?
I generally think of Cucumber as a way to do integration testing on your application. Combined with Webrat, you can test user workflows, views and so on in a great way. For unit tests, you'll want to go down to a lower level and test your models just with rspec. You may also want to do some functional tests on the controllers, and I probably wouldn't use Cucumber for that either.
Here are a couple of videos:
http://confreaks.com/videos/72-mwrc2009-bdd-with-cucumber
http://rubyconf2008.confreaks.com/rspec-and-cucumber.html
Ryan Bates has some good Railscasts on these topics:
Beginning with Cucumber
Webrat
More on Cucumber
This may be a matter of taste, but having tried out Rspec I prefer using the built-in Rails testing framework along with a gem called Shoulda. In my opinion, that combination lets you write much clearer, more succinct and understandable tests than Rspec by far. But not everyone would agree.
Shoulda's contexts let you organize your tests into logical hierarchies which really helps when you're trying to test all the possible paths some crazy, branching situation, like user logs in with right pw, wrong pw, right pw but registration not confirmed, etc.
In addition be sure to install the ZenTest gem. That lets you just execute the command $ autotest and your tests will run automatically every time you change a file.

What is the Path to Learn BDD on Ruby On Rails?

I want to start BDD on Ruby On Rails what should I learn?
I don't know anything about BDD, RSpec or Cucumber. What is the best way to learn? Tutorials? Something that cover things like 'What behavior I should test?' etc.
thanks!
RSpec specific; I keep these links for reference (might be advance for beginners). But just wanna share it with you. (Some of it probably already outdated but I think still worth a read)
Rspec Link Fest - links to other references
RSpec in Controllers:
Testing your Application Controller with rSpec
Testing Controllers with rspec
Using Rspec on Controllers
RSpec in Models:
stub_model by Dave Chelimsky
Do we really need Controller and View tests? - some discussion
RSpec in Views:
RSpec testing views for escaped HTML
Rspec Stories (now Cucumber):
Understanding RSpec Stories - a Tutorial
Rspec on Windows:
RSpec, autotest and Snarl on Windows - dunno if this still relevant
p/s: Some are from 2007, I'm not sure if the stuffs are outdated or not, but these are the some of my references used to learn RSpec. Hope it helps
Start with two articles by Dan North: Introducing BDD, followed closely What's in a Story?
Start at the top, RSpec home page:
http://rspec.info/

Resources