Everyone is talking about TDD (BDD) in Rails (and not just Rails) development world today. It's easy to find plenty of good general information about it, there are quite a few tools you can use for this purpose and there are many (good) examples of how to use them.
Now, I'm already on the train. I like the idea (never did TDD before) and I decided to go for Shoulda (because it seems to me quite easy to understand). I read plenty of examples and have done some first tests. My problem is this: I'm just not sure if I'm doing it right. As I said, there are many tiny examples all over the net, but I haven't found any comprehensive guide covering overall workflow of TDD with Shoulda.
Here are my questions:
What is your way of practising TDD with Shoulda? (What is your usual workflow throughout one iteration?)
What other tools do you use (except of Shoulda itself)?
Any other tips?
I'm using RSpec instead of shoulda, (although I believe that shoulda can be used with RSpec) and the biggest improvement to my workflow has come from using Autotest and then RSpactor.
These tools automatically monitor your source tree for changes and execute the relevant tests if update either the test or implementation file. A pretty small change to the testing practice but the immediate feed back has increased my discipline in writing tests for every piece of functionality.
+1 for Autotest.
If you have a large test suite and only want to run the test you are working on (as opposed to the whole test suite) check out this monkey patch for Autotest.
As for other tools you should check out Factory Girl as a fixtures replacement.
Most of what I know about using Shoulda came by reading others test code. Check out some of Thoughtbot's projects on github, such as Clearance and Pacecar.
I totally agree that there is a significant gap in the rails BDD documentaion. Lots of little examples but no big ones giving an overall picture of how to do it. Even the books that have chapters on testing are guilt of this. The only full examples I have seen are peepcode's three part screencast on rspec and a video on confreaks about flex mock.
I'd love to hear about other good resources on BDD that go beyond minor examples of syntax and I'd really like see a book on the topic.
Related
Now I'm using cucumber, but I find it a little oververbosity. I'm not going to show cucumber futures to somebody without Ruby knowledge. So now I'm thinking that it isn't necessary to duplicate integration test in human-readable text and in step definitions. It makes me to do useless job.
I'm in the middle of my way to something more geeky than cucumber. Something like rspec integration but with power no less than cucumber power.
Could you advice some tool for integration testing for my purposes?
Try Spinach :)! I have the feeling it is exactly what you are looking for!
RSpec feature specs are fairly analogous to Cucumber integration tests. This requires the Capybara gem.
Moving your tests back to RSpec keep them more code-like, and you remove the extra step of needing to parse gherkin. This is a good approach if you don't really need to be sharing the specs with non-technical folks.
Thoughbot has a post that illustrates an interesting actor-based approach to feature specs: End-to-end testing with RSpec integration tests and Capybara.
The upcoming Rails 4 in Action book describes how to do feature specs in great detail. There is a GitHub repo for the code referenced in the book (the sample app is a project-tracking system) if you want to take a look.
For the sake of completion, you should also consider turnip, though it seems very similar to spinach; deciding factor for me for choosing turnip was the seeming lack of placeholders in Spinach, which I have a hard time understanding.
Have you used Cucumber in a production environment?
What problems did you find using it?
Would you recommend I use it?
I'm currently learning to use Cucumber with RSpec, I'm weighing up Pro's and Con's for using it in upcoming projects and would like some input. I will be using RSpec but I'm wondering if using Cucumber will be worth the extra time taken to write the tests.
Extra Information:
I don't deal with clients directly; however I do work in a team and deal with project managers who don't have much technical knowledge so I thought the readability of Cucumber tests would prove useful
The project sizes are medium to large, projects can have multiple phases and we maintain them
Time restrictions are pretty tight however I’m thinking the extra test coverage would hopefully catch more bugs reducing time overall
Any information would be great
Cheers
Personally I don't use Cucumber.
I imagine there are great use cases but I find the idea of writing plain english 'cukes' - that I then have to essentially write ruby regular expressions for to turn the plain english tests into ruby that can be run just too much of an overhead. That said, if you have business analysts or even the customer able to write tests then Cucumber is great since they can write english and you just have to worry about making it work.
We've opted to use Steak which is built straight on top of Rspec2 and Capybara and lets us write straight ruby tests which we feel are just as descriptive without the extra overhead.
I want to learn how to write unit tests for a Rails App. Where do I start? All the tutorials on Google are old, back from 2007, and there seem to be an abundance of choice but not a preferred solution.
Right now most people seem to be using Rspec for unit testing and Cucumber for integration testing. You can see a fairly recent poll here where 87% chose Rspec in a survey.
A great book for Rspec and Cucumber is The Rspec Book written by the current maintainer of Rspec. It goes over both Rspec and Cucumber.
Railscasts also has a few relevant screencasts. Cucmber1, Cucumber2
Many people seem to love Cucumber but it doesn't seem as useful if you don't have a client you're doing work for. Most of my projects are side projects I do myself so these days I'm looking into Steak instead of Cucumber. This will allow me to use Rspec for unit tests and Steak for integration tests and reduces some complexity/tedium that is introduced when using Cucumber.
People have said that it doesn't really matter which testing framework you pick, it's more important that you START TESTING. I agree with that but hopefully these resources will help you get started.
However, one thing you probably want to avoid from the very start are fixtures. Use factories instead and check out this Railscasts episode on it.
UPDATE: Steak is no longer necessary and the same functionality is baked into Rspec.
I'd suggest starting with the Rspec Book by David Chelimsky and friends. It goes over the various types of testing, why you should test and runs through various examples that show you good practices as well as giving you guidelines for what to avoid.
Rspec is very popular and preferred by many over the default rails test suite. Most material I've read that goes over using the default test suit ends with an introduction to Rspec and how it makes life simpler.
If you want to do it the proper way, you should use others testing frameworks like Rspec, Cucumber or shoulda. This way you will write Unit Tests in a BDD context. Those frameworks are recent so obviously the documentation that you will find about them are recent enough! Finally, I'd suggest you to read this great blog post from Sarah Mei about the outside-in approach combining Rspec/Cucumber, that's how I was convinced to use these frameworks, she really explains very well how you should approach the tests!
So yeah, I'm gettin' into this testing thing!
But I don't know what to use =/
Rspec + Shoulda?
Rspec + Steak?
Minitest?
Cucumber?
Capybara?
Coulda? (Cucumber with Shoulda mixup)
Mini_shoulda? (Minitest with Shoulda mixup)
Argh, so many choices! I'm confused <.< I do know I want something small and simple to test my future gems and sinatra & rails apps. What do you people use and why?
If you are just starting out, you should go with the defaults and follow the official guides. The important thing is that you practice testing. It's less important which testing library/framework you use - that's a stylistic choice that's secondary to actually practicing testing in the first place - and the easiest way to get starting testing your Rails app is to follow the "Golden Path" that the official guides lay out.
Once you get more comfortable with testing your Rails app, you will be in a much better position to evaluate the options out there. I personally like rspec and cucumber (at the moment - I may decide to change my preference), but I was only able to come up with this preference after already being somewhat familiar with testing and after already being somewhat familiar with Rails.
I just came across this blog post which states:
It seems that the accepted way to test in Ruby is to use Rspec for unit tests and to use Cucumber for acceptance tests (higher level functional testing).
It then goes on to ask: "if we agree that BDD is good, why don’t we write our unit tests in a format that is more amenable to BDD, that being the Cucumber format of tests?" and compares rspec unit tests against unit tests in Cucumber.
At work we use the pretty standard RSpec and Cucumber combination. I don't know how they decided on this specific combination, it was already there when I started and worked for us, so there was no need to change. Also it's a quite widely use combination so finding examples with Google etc. isn't too hard.
For my 1.9 based private projects I think I will use Minitest from now on. No external dependencies, a simple BDD DSL (require 'minitest/spec') and some other niceties like randomization. Here's a nice quick intro:
http://www.bootspring.com/2010/09/22/minitest-rubys-test-framework/
For someone just getting started I recommend just using RSpec. It doesn't do everything but it will allow you to build up reasonable sets of unit and integration tests. When you run into the limits of what RSpec handles easily then let that need guide you in choosing additional tools.
Can't offer anything more sensible than Justice, but before you ignore that wisdom :-) do check out this video (minitest author Ryan Davis at Cascadia 2011 ruby conf):
http://confreaks.net/videos/618-cascadiaruby2011-size-doesn-t-matter
slides: http://www.zenspider.com/~ryan/presentations/CascadiaRubyConf_2011_-_Size_Doesn%27t_Matter.pdf
Being able to run all your tests very quickly is a very good thing.
I am setting up a rails app and I just finished making some unit tests and my friend said that apparently fixtures are no longer cool and people are now using RSpec or shoulda. I was wondering what the actual benefits are to use these other toolkits. Any information at all is appreciated.
-fREW
I personally prefer Shoulda to RSpec. I find that Shoulda has less magic syntax than RSpec. My problem with RSpec is that yeah it's very readable when I read it aloud, but when I get to writing it, hmmmm, I'm never sure how a given assertion should be written. Prag Dave explains the problem better than me. He also likes Shoulda and has a few examples.
RSpec and similar frameworks are tooling designed to aid in Behavior Driven Development. They're not just a prettier way to write tests, though they do help with that.
There is plenty of information on BDD here: http://behaviour-driven.org/
And wikipedia: http://en.wikipedia.org/wiki/Behavior_Driven_Development
There are too many benefits to list here, so I'd recommend browsing that site a little.
There are two different things here:
The first thing is what framework to use for writing tests/specs. Here you can choose between Test::Unit, RSpec, Shoulda and so on. The choice is whether you want to do traditional TDD (Test::Unit) or whether you prefer the alternative ways of thinking about specifiying behaviour advocated by developers like David Chemlinsky (RSpec and to some extent Shoulda).
The second thing is how to handle test data. There are Rails fixtures and alternatives desgined with other goals such as the FixtureReplacement plugin. Before Rails 2.0 fixtures had significant and well-documented pratical problems. Many of the practical issues were fixed in Rails 2.0. However fixtures can lead to inadvertent test coupling and some of the alternatives try to avoid this.
RSpec is way more powerful because it's far easier to both read and write tests in. It's also very elegant when using mocks and stubs, a concept which will become extremely useful once you start using them in your tests. Try it in a simple test app (NON RAILS!) and you'll see how elegant your specs are versus the equivalent standard testing.
Check out Josh Susser's The Great Test Framework Dance-off for a comparison of the popular Ruby testing frameworks.
If you are building a large application and don't have a team that are all really good at writing decoupled code that can be well-tested with black box tests and are prepared to fully embrace using/debugging lots of mocks & stubs, don't go down the Factory road.
Wherever you read about how Awesome Factories Are you'll see a little caveat about how factories might not be feasible in a large application because they are a little slower than fixtures.
But "a little slower" is really orders of magnitude slower.
Factories are not significantly easier to code than fixtures that use labels for ids, so long as you keep the fixtures organized. And in some cases factories are harder to debug.
Just tonight I converted a single factory to fixtures, and the runtime of the test file that used it went from 65 seconds to 15 seconds, even though only about 15% of the tests in that test file use that factory.
If you use minitest you can run your tests in random order; this will quickly reveal any data coupling between tests. (not sure if rspec has the option to randomize test order)
Test::Unit is good for small applications. But there are a lot of benefits to use testing frameworks like Shoulda or RSpec, e. g. contexts!!
I don't see Shoulda and RSpec in an either-or-relation. I use Shoulda as a substitute for RSpec when it comes to single-assertion testing. I really like the Shoulda one-liners, but writing matchers is much easier in RSpec. So my advise is to use the different testing tools where they fit best.
You may use testing framework like Cucumber which is even more faster than RSpec..