25

I'm looking for a rather recent open source application that uses Rspec 2 as test library. I'd like to see how an experienced developer utilizes the library properly to test the full stack, since I'm constantly in doubt concerning my own knowledge (coming from testunit and partly due to the rather sparse documentation of the latest Rspec release, even though it is constantly improved).

If a project would use Cucumber, Pickle and/or Capybara as well together with Rspec 2, you'd have me jumping for joy.

Any pointers?

Cheers!

polarblau
  • 17,649
  • 7
  • 63
  • 84

1 Answers1

54

My 2 cents:

Use Steak instead of Cucumber. It RSpec at its core, it is simple and it does the job.

https://github.com/cavalle/steak

Capybara allow you use different drivers. Some drivers support javascript, run with a browser, faster, slower, etc. Use the best Driver for the spec you are testing using Swinger:

https://github.com/jeffkreeftmeijer/swinger

I use my own fork of Akephalos – a driver – which is fast, support javascript, UTF-8 (that's what my fork adds) and doesn't need an external browser.

https://github.com/Nerian/akephalos2

A good practice for RSpec is to use 'Context'. Ask me if you need clarification. Also, take note of the let method. It returns whatever the block returns. It is useful for declaring mock a object inside and using them on the samples. .

feature "Course" do

  let(:school) {School.make!}

  context "Loged in" do
    before(:each) do
      switch_to_subdomain(school)
    end

    context "In the new course form" do
      before(:each) do
        click_link("Courses")
        click_link("New course")
      end

      scenario "New course" do               
      end

      scenario "A Course without name should not be accepted" do
      end

      scenario "A new course should not be created if there is another one with the same name in the same school" do
      end
    end
  end  
end   

Also, the book: The RSpec Book, of Pragmatic Programmers is a very good resource for initiating yourself about the core concepts behind RSpec, Capybara, Cucumber and all this Behaviour Driven Development agile thing :)

Edit:

Also, I use Machinist2 for fixtures. https://github.com/notahat/machinist

Works great. Better than Factory girl.

There is also Fabricator, which have an excellent website and a very usable DSL.

https://github.com/paulelliott/fabrication

You can use Machinist with Forgery in order to create intelligent data.

https://github.com/sevenwire/forgery

 School.blueprint do
    name { "Pablo de olavide"}
 end

 Student.blueprint do
    first_name { Forgery::Name.first_name}
    last_name { Forgery::Name.last_name }
    school { School.make! }
 end

You can combine this with a Thor task in order to populate you development database, to see the application as the final user would see it.

def populate        
    require File.expand_path('config/environment.rb')
    require File.expand_path('spec/support/blueprints.rb')        
    drop
    puts "populating database"
    1.times do |num|
       school = School.make!
       50.times do
       Student.make!(:school => school)

       end                                             
    5.times do        
       Course.make!(:school => school)          
       Professor.make!(:school => school)                
       end            
    end
end

The documentation of RSpec 2 has many examples:

http://relishapp.com/rspec

Also, this Post give many other tips:

http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/

Another post with very good advise:

http://flux88.com/2011/05/dry-up-your-rspec-files-with-subject-let-blocks/

Optimising the execution time of tests:

http://blog.leshill.org/blog/2011/10/23/fast-specs.html

http://jeffkreeftmeijer.com/2011/spec-helpers-bundler-setup-faster-rails-test-suites/

Nerian
  • 15,901
  • 13
  • 66
  • 96
  • Thanks a bunch for the input! I'm currently using Factory Girl but I think I might give Machinist a try in the near future. – polarblau Jan 06 '11 at 18:41
  • … I've read about Steak and it sure seems closer to my own "style" (hä?), but I'd like to use Cucumber once across one full project to get a better impression. I had thought about buying the RSpec book as well, thanks for the hint, I think I might just go for it then! And I'll definitely try "context". Thanks again. — Nevertheless anyone with some open project I could have a peek at? – polarblau Jan 06 '11 at 18:47
  • 1
    You see, one really good place to watch RSpec2 specs done by the masters is the RSpec2 git repository itself :) Cucumber: https://github.com/dchelimsky/rspec/tree/master/features and RSpec; https://github.com/dchelimsky/rspec/tree/master/spec – Nerian Jan 06 '11 at 18:52
  • Whenever you think someone gave a good answer or comment, give him a Vote. Thank you. – Nerian Jan 06 '11 at 18:54
  • +1, but deserves more plusses. Outstandingly comprehensive; great links and examples. – Wayne Conrad Jan 06 '11 at 20:32
  • 1
    Sorry @Nerian, my reputation is still to low to do give you a vote up. Will do so as soon as I can. Bought the rspec book now. Looks great. Still looking for a more complex real–world example in the wild, though. Thanks again! – polarblau Jan 09 '11 at 16:59
  • After reading a good part of the excellent rspec book I'll accept this answer now. It doesn't really answer my question directly, but it gave me what I was after in the long run (or I hope that it will at least). And there's now question about the quality of the answer in any case. – polarblau Jan 26 '11 at 11:18
  • @polarblau: I added at the end a link with more tips. – Nerian Mar 11 '11 at 20:20
  • @polarblau: I added another good rspec post at the end :) – Nerian May 15 '11 at 12:34
  • @Nerian: IIRC users require at least 15 rep to upvote answers, even to their own questions. – Andrew Grimm May 15 '11 at 23:43