Системное тестирование приложений на Ruby on Rails с применением Rspec и Capybara
RSpec and Rails
-
Upload
alan-hecht -
Category
Technology
-
view
5.171 -
download
2
description
Transcript of RSpec and Rails
![Page 1: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/1.jpg)
RSpec and Rails
Alan Hechthttp://alanhecht.me
![Page 2: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/2.jpg)
Installation
• RSpec 2.5– Only works with Rails 3– rspec and rspec-rails– gem install rspec-rails
• What if you’re using Rails 2.x?– Need to install RSpec 1.3
![Page 3: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/3.jpg)
Creating a Rails Project
• rails new cart_rspec
• Edit the Gemfile
• bundle install• rails generate rspec:install
![Page 4: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/4.jpg)
Railswizard.org
• Railswizard.org allows you to specify options when creating a Rails 3 project
• Rails 3 allows for flexibility in what’s used– Database/ORM • ActiveRecord, Mongomapper
– Unit Testing Framework• TestUnit, Rspec
– JavaScript Framework• jQuery, Prototype
![Page 5: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/5.jpg)
Set up the project
• Use railswizard to create a template– We’ll be creating the shopping cart
• Create rvmrc file
• bundle install
• Git setup
![Page 6: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/6.jpg)
Set up the Product & Cart
• Generate product scaffolding• Generate cart scaffolding• Generate line_item scaffolding
• RSpec tests were created with scaffolding
• Commit changes
![Page 7: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/7.jpg)
RSpec - Models
![Page 8: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/8.jpg)
Run Tests
• Update database– rake db:migrate
• Run RSpec– rake spec
![Page 9: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/9.jpg)
RSpec Options
• In .rspec file– Documentation format• --format doc
– HTML format• --format html:/path/to/file
![Page 10: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/10.jpg)
RSpec – Models
• Most similar to using RSpec without Rails– Everything we learned from the calculator
example applies
• Business logic testing should take place in the models
![Page 11: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/11.jpg)
RSpec - Models
![Page 12: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/12.jpg)
RSpec – Fixtures
• Fixture files go in spec/fixtures
• Need to explicitly load fixtures
• Can load all the fixtures for tests– Add ‘config.global_fixtures = true’ to
spec_helper.rb
![Page 13: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/13.jpg)
RSpec - Controllers
• In Rails Test::Unit, Controllers and Views tested together in functional tests
• For RSpec, each is tested separately
• Outer describe contains the controller class– Needed to run the tests
• Describe in each test has controller action– Doesn’t affect the test, only by convention
![Page 14: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/14.jpg)
Controller Matchers
• RSpec has matchers specifically for controller testing:– response.should be_success– response.should be_redirect– response.should redirect_to(url)– response.should
render_template(‘app/views/products/index’)– response.should render_template(:partial =>
“show_cart”)
![Page 15: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/15.jpg)
Controllers - Example
![Page 16: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/16.jpg)
Stubs – What are they?
• A fake object that returns a pre-determined value for a method call– Actual object is not called
• Stubs and mocks often used interchangeably– Mean two different things– But people generally refer to “Mock Object
Frameworks”
![Page 17: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/17.jpg)
Stubs – Why?
• Separation of concerns• Controller doesn’t depend on model• Models don’t depend on each other– Each model can be tested in isolation
• Don’t need the database for testing– Faster tests
• Could use stubs to simulate a network or web service response
![Page 18: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/18.jpg)
Stubs – Implications
• Need to have integration tests– All of your tests could pass, but the site may not
function– Webrat (used by Cucumber) & Capybara are two
such integration tools
![Page 19: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/19.jpg)
Mocks
• Just like Stubs, but with an expectation to be (or not to be) called
• Test will fail if the mock is (or isn’t) called
• Used in the same way as stubs
![Page 20: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/20.jpg)
RSpec - Views
• Verify the presence of fields on the view
• Avoid relying on the layout of the view– View could break from a design change– But good for testing groups of elements with a
common purpose (like options fields that expand or hide)
• Use XPath or CSS selectors to locate tags– Strongly recommend CSS selectors
![Page 21: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/21.jpg)
RSpec - Views
![Page 22: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/22.jpg)
RSpec - Views
• Describe contains view path– Optional but useful– If ‘render’ doesn’t contain arguments, use the
comment on the describe line
![Page 23: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/23.jpg)
Test Coverage
• rcov gem will show you how much of your code has been tested
• rake spec:rcov– By default, all tests are run– Need to modify Rakefile to cover only specific
areas like models & views• Output is in the <project>/coverage directory– HTML viewable in a browser– Open coverage/index.html
![Page 24: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/24.jpg)
RCov - Output
![Page 25: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/25.jpg)
RCov - Tips
• No need to try for 100% test coverage
• Good goal would be around 70 – 80%
![Page 26: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/26.jpg)
Automatic Testing
• Autotest monitors changes to files in your project
• Every time a test is saved, the test is re-run– Everytime app code is saved, corresponding test is
run• gem install autotest & autotest-rails-pure• Create an autotest directory– discover.rb
![Page 27: RSpec and Rails](https://reader033.fdocuments.net/reader033/viewer/2022061218/54b718f24a7959af718b466e/html5/thumbnails/27.jpg)
Autotest – Tips
• Be careful when using on a laptop– Autotest polls for changes– You’ll either get burnt by a hot laptop or your
battery will die, whichever comes first– On OS X, you can install autotest-fsevent, the file
system change triggers autotest execution• Growl notifications– autotest-growl
• Add to rails root .autotest file