Basic RSpec 2

  • 885 views
Uploaded on

Authors: Triet Le - Truc Nguyen

Authors: Triet Le - Truc Nguyen

More in: Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
885
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
30
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. The Basic of RSpec 2Authors: Triet Le – Truc Nguyen
  • 2. Agenda•TDD, BDD intro•RSpec intro•Expectation - Matcher•Mocking-Stubing•RSpec Rails•Controller Specs•Model Specs•Rspec vs Cucumber•Feature Specs•Code coverage tool: SimpleCov, Rcov•Example Code
  • 3. Test Driven Development
  • 4. Source: http://centricconsulting.com/agile-test-driven-development/
  • 5. •Its tests that drive the development•Make sure that No code goes into productionwithout associated tests•Benefits of TDD:http://agilepainrelief.com/notesfromatooluser/2008/10/advantages-of-tdd.htmlTDD
  • 6. Behavior Driven Development
  • 7. BDD•BDD is based on TDD•BDD is specifying how your application shouldwork, rather than verifying that it works.•Behaviour-Driven Development is aboutimplementing an application by describing itsbehavior from the perspective of itsstakeholders. (Rspec book)
  • 8. RSpec?
  • 9. RSpec•Rspec is unit-testing framework for Rubyprogramming language•RSpec is BDD•Rspecs strongly recommended with TDD
  • 10. How
  • 11. describe `Class` dobefore do# Setup somethingendit “should return something“ do# actual_result.should matcher(expected_result)endendHOWAn ExampleExamplegroupThis is a Spec file
  • 12. Expectation - Matcherhttp://rubydoc.info/gems/rspec-expectations/frames
  • 13. Expectation - Matcher•Basic structure of an rspec expectationo Old syntax actual.should matcher(expected) actual.should_not matcher(expected)o New expect(actual).to eq(expected) expect(actual).not_to eq(expected)•For exampleo 5.should eq(5) / expect(5).to eq(5)o 5.should_not eq(4) / expect(5).not_to eq(5)
  • 14. For Example:
  • 15. Mocking - Stubbinghttp://rubydoc.info/gems/rspec-mocks/frames
  • 16. Mocking - StubbingRspec-mocks is a test-double framework forrspec with support for methodso mock a `fake` objecto stubso message expectations on generated test-doubles andreal objects alike.
  • 17. Mocking - Stubbing•Test doubleo book = double("book")•Method Stubso book.stub(:title) { "The RSpec Book" }o book.stub(:title => "The RSpec Book")o book.stub(:title).and_return("The RSpec Book")•Message expectationso person = double("person")o Person.should_receive(:find) { person }•should_receive vs stub
  • 18. Mocking - Stubbing•Good foro Speed up testingo Real object is unavailableo Difficult to access from a test environment: Externalserviceso Queries with complicated data setup
  • 19. Mocking - Stubbing•Problemso Simulated API gets out of sync with actual APIo Leads to testing implementation, not effecto Demands on integration and exploratory testing higherwith mockso Less value per line of test code!
  • 20. RSpec-Railshttps://www.relishapp.com/rspec/rspec-rails/docs
  • 21. Rspec-rails•Add to Gemfile•group :test, :development dogem "rspec-rails", "~> 2.4"end•RecommendedoFactory-girloGuard-specoSporko SimpleCov
  • 22. Rspec-railssource: http://www.rubyfocus.biz/ViewsController RoutesApplication, Browser UIApplication, ServerHelpersModelSeleniumRSpec Integration/Request,Cucumber, Webrat, CapybaraRSpec Views RSpec HelpersRSpecControllerRSpec RoutingRSpec Model Test::UnitTest::UnitFunctionalTest::UnitIntegrationApplication, Browser UIApplication, Server
  • 23. https://www.relishapp.com/rspec/rspec-rails/v/2-13/docs/controller-specsController Specs
  • 24. Controller specs•Simulate a single HTTP verb in each exampleo GETo POSTo PUTo DELETEo XHR•Accessable variableso controller, request, responseo assigns, cookies, flash, and session
  • 25. Controller specs•Check renderingoCorrect template response.should render_templateoRedirect response.should redirect_to (url or hash)o Status code response.code.should eq(200)•Verify variable assignmentso Instance variables assigned in the controller to beshared with the viewo Cookies sent back with the response cookies[key] cookies[key]
  • 26. What need to test in model?Model Specs
  • 27. Model specs•Exists attributes•Association•Model’s validations•Class methods•Instance methods
  • 28. Model specsFor detail, a model spec should include:•Attributeso model attributes should have•Associationo model association should have•The model’s create method -> check thevalidation work?o passed valid attributes => should be valid.o fail validations => should not be valid.•Class and instance methods perform asexpected
  • 29. Model specs - Example code
  • 30. code example
  • 31. Model specs - Example rspec modelcode
  • 32. fields, association,validations
  • 33. The model’s method create
  • 34. class andinstance method
  • 35. Same and differenceRspec vs Cucumber
  • 36. Rspec vs Cucumber•Both testing frameworks.•Both are used for Acceptance Testing•These are business-case driven IntegrationTestso simulate the way a user uses the application,o the way the different parts of your application worktogether can be found in a way that unit testing will notfind.same
  • 37. Rspec vs Cucumber•RSpec and Cucumber are the businessreadability factordifferenceCU CU M B ERodraw is that thespecification (features)from the test codeoproduct owners canprovide or review withouthaving to dig through codeR SPECodescribe a step with aDescribe, Context or Itblock that contains thebusiness specificationoa little easier fordevelopers to workobut a little harder fornon-technical folks
  • 38. Example
  • 39. Integration test with rspec and capybara(and Senelium)Feature Specs•Introduce•Setup env•Example code
  • 40. Feature Specs - Introduce•high-level tests meant to exercise slices offunctionality through an application. Drive theapplication only via its external interface,usually web pages.•Require the capybara gem, version 2.0.0 orlater. Refer to the capybara API for more infoson the methods and matchers•Feature, scenario, background,given DSL <=>describe, it, beforeeach, let. Alias methods that allow to readmore as customer tests and acceptance tests.
  • 41. Feature Specs - Setup envFirst, add Capybara to yourGemfile:In spec/spec_helper.rb,add two require calls forCapybara near the topCapybara’s DSL will beavailable spec/requestsand spec/integrationdirectory
  • 42. Feature Specs - SeleniumFirst, add Capybara to yourGemfile:In spec/spec_helper.rb,add two require calls forCapybara near the top•Run Selenium, just set :js => trueFirefox shouldautomaticallyfire up and runwith Selenium.•Capybara.default_driver = :selenium (makeCapybara to all your tests - don’t recommend)•Using Rack::Test (default driver) and Selenium(JS driver) by setting the :js attribute (faster if useSelenium for tests that actually require JS)
  • 43. Feature Specs - Sample codeWhen writing integration tests, try to model the test around an actor (user of the system) andthe action they are performing.
  • 44. Feature Specs - Sample code
  • 45. code coverage toolSimpleCov, Rcov
  • 46. Demo•Model spec•Feature specs
  • 47. Thank You!
  • 48. •New syntax expectation rspec•Feature specs•Rspec vs cucumber•http://www.slideshare.net/NasceniaIT/tdd-bdd-r-spec•Intergartion test•Setup capypara•Devise with rspecReferences