Testing with Rspec 3

Post on 16-Aug-2015

38 views 1 download

Tags:

Transcript of Testing with Rspec 3

Testing with RSpec 3David PaluyJul 2015

Agenda

● RSpec intro● Expectation - Matcher● Mocks vs Stubs● Model Specs● Controller Specs● FactoryGirl● API specs

RSpec 3

Start from reading other people's code

Read:https://relishapp.com/rspechttp://betterspecs.org/

RSpec 3

RSpec 2

Subject

let

RSpec Expectations

http://www.rubydoc.info/gems/rspec-expectations/frames

Mocks & Stubs

http://www.rubydoc.info/gems/rspec-mocks/frames

RSpec with Rails

RSpec Rails

Model spec

● Attributes existence

● Association

● Validation

● Class & Instance methods

Controller spec

https://www.relishapp.com/rspec/rspec-rails/v/2-13/docs/controller-specs

Controller spec

Simulate HTTP request:GET, POST, PUT, DELETE, XHR

Variables:controller, request, response,assigns, flash, session

Factories vs Fixtures

Factories

● YML files loaded on start

● Bypass ActiveRecord

● Easy to generate from Real Data

FactoryGirl

FactoryGirl - instantiation options

❖ create: saves object to the database, and saves any associated objects too

❖ build: builds object in memory only, but still saves any associated objects to the database

❖ build_stubbed: builds object in memory only, as well as any associated objects

API Testing - best practices

HTTP response status codeResponse body

API Testing is Integration Testing

RSpec Request specs

JSON Helper for RSpec

https://gist.github.com/dpaluy/b43821d086211bb10cda