Well

1,005 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,005
On SlideShare
0
From Embeds
0
Number of Embeds
61
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Well

  1. 1. Test Driven Development and Rspec 2 Breccan McLeod-Lundy @breccan
  2. 2. Test Driven Development RED GREEN REFACTOR
  3. 3. A Simple Spec describe ProjectsController do context "logged in user" do before(:each) do @user = User.make ApplicationController.stub(:current_user).and_return(@user) End it "should do something" do @user.should_receive(:do_stuff).and_return(true) post :do_stuff end end end
  4. 4. Mocks and Stubs Double('classname) mock_model(Model, :variables) thing.stub(:action) { 'value' } Thing.stub_chain(:meth1, :meth2).and_return(cake) double('topic', :title => 'front')
  5. 5. Methods: .should() .should_not() # NOT != .should_receive() .with() .and_return() Actions post :action, :param => “value”
  6. 6. Expectations be_something? #Checks for method something? and calls it. be_valid #Checks for validity
  7. 7. Expectations(cont) .ordered .should_receive(1, 2, 3) .should_receive(“cake”).once .exactly .at_least etc
  8. 8. Expectations(cont) have_key = has_key? have(10).somethings match(/cake/) Thing.stub_chain(:meth1, :meth2).and_return(cake) double('topic', :title => 'front') raise_error(CakeIsLieError) throw_symbol(:cake) be_close(:expected, margin)
  9. 9. Rspec.configure Rspec.configure do |c| c.filter = { :important => true } c.exclusion_filter = { :slow => true } End #Runs this block describe “tests”, :important => true do it “does something” do end #excludes this test it “does something slow”, :slow => true do end End #You can put lambdas in there too.
  10. 10. Custom Matchers RSpec::Matchers.define :be_in_zone do |zone| match do |player| player.in_zone?(zone) end failure_message_for_should do |player| # generate and return the appropriate string. end failure_message_for_should_not do |player| # generate and return the appropriate string. end description do # generate and return the appropriate string. end end
  11. 11. Macros config.extend(ControllerMacros, :type => :controller) # in your Spec::Runner config Say you want: get :index do should_render "index" should_do_stuff end
  12. 12. Create a new ControllerMacros module with methods that construct appropriate it() statements. And add something like: def get(action) define_method :do_request do get action end yield end
  13. 13. Capybara within("li#employee") do fill_in 'Name', :with => 'Jimmy' End save_and_open_page #opens a snapshot of page Capybara.default_wait_time #lets you set how long to wait between actions for ajax when you're checking content.
  14. 14. Capybara + WebDriver $ sudo gem install cucumber cucumber-rails $ ./script/generate cucumber --capybara --rspec $ RAILS_ENV=cucumber rake gems:install Capybara.default_driver = :selenium Or use tags(@javascript etc) and only use selenium for relevant tests: Capybara.javascript_driver = :selenium
  15. 15. Other Important Things Rspec is now in pieces Cucumber Autotest CI
  16. 16. Test Driven Development and Rspec 2 Breccan McLeod-Lundy @breccan P.S. Don't forget Railscamp http://railscamps.com

×