basic rspec 2

49
The Basic of RSpec 2 Authors: Triet Le – Truc Nguyen

Upload: triet-leminh

Post on 12-May-2015

1.092 views

Category:

Education


2 download

DESCRIPTION

Authors: Triet Le - Truc Nguyen

TRANSCRIPT

Page 1: Basic RSpec 2

The Basic of RSpec 2Authors: Triet Le – Truc Nguyen

Page 3: Basic RSpec 2

Test Driven Development

Page 4: Basic RSpec 2

Source: http://centricconsulting.com/agile-test-driven-development/

Page 5: Basic RSpec 2

•It's 'tests' that 'drive' the 'development'•Make sure that No code goes into production

without associated tests•Benefits of TDD:

http://agilepainrelief.com/notesfromatooluser/2008/10/advantages-of-tdd.html

TDD

Page 6: Basic RSpec 2

Behavior Driven Development

Page 7: Basic RSpec 2

BDD

•BDD is based on TDD•BDD is specifying how your application should

work, rather than verifying that it works.•Behaviour-Driven Development is about

implementing an application by describing its behavior from the perspective of its stakeholders. (Rspec book)

Page 8: Basic RSpec 2

RSpec?

Page 9: Basic RSpec 2

RSpec

•Rspec is unit-testing framework for Ruby programming language

•RSpec is BDD•Rspec's strongly recommended with TDD

Page 10: Basic RSpec 2

How

Page 11: Basic RSpec 2

describe `Class` dobefore do # Setup something

end

it “should return something“ do # actual_result.should matcher(expected_result)

endend

HOW

An ExampleExample group

This is a Spec file

Page 12: Basic RSpec 2

Expectation - Matcherhttp://rubydoc.info/gems/rspec-expectations/frames

Page 13: Basic RSpec 2

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)

Page 14: Basic RSpec 2

For Example:

Page 15: Basic RSpec 2

Mocking - Stubbinghttp://rubydoc.info/gems/rspec-mocks/frames

Page 16: Basic RSpec 2

Mocking - Stubbing

Rspec-mocks is a test-double framework for rspec with support for methodso mock a `fake` objecto stubso message expectations on generated test-doubles and

real objects alike.

Page 17: Basic RSpec 2

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

Page 18: Basic RSpec 2

Mocking - Stubbing

•Good foro Speed up testingo Real object is unavailableo Difficult to access from a test environment: External

serviceso Queries with complicated data setup

Page 19: Basic RSpec 2

Mocking - Stubbing

•Problemso Simulated API gets out of sync with actual APIo Leads to testing implementation, not effecto Demands on integration and exploratory testing higher

with mockso Less value per line of test code!

Page 20: Basic RSpec 2

RSpec-Railshttps://www.relishapp.com/rspec/rspec-rails/docs

Page 21: Basic RSpec 2

Rspec-rails

•Add to Gemfile•group :test, :development do gem "rspec-rails", "~> 2.4"end

•Recommended oFactory-girloGuard-spec oSpork o SimpleCov

Page 22: Basic RSpec 2

Rspec-rails

source: http://www.rubyfocus.biz/

Views

Controller Routes

Application, Browser UI

Application, Server

Helpers

Model

Selenium

RSpec Integration/Request, Cucumber, Webrat, Capybara

RSpec Views RSpec Helpers

RSpec Controller

RSpec Routing

RSpec Model Test::Unit

Test::Unit Functional

Test::Unit Integration

Application, Browser UI

Application, Server

Page 23: Basic RSpec 2

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

Controller Specs

Page 24: Basic RSpec 2

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

Page 25: Basic RSpec 2

Controller specs

•Check renderingoCorrect template response.should render_template

oRedirect 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 be

shared with the viewo Cookies sent back with the response

cookies['key'] cookies['key']

Page 26: Basic RSpec 2
Page 27: Basic RSpec 2

What need to test in model?

Model Specs

Page 28: Basic RSpec 2

Model specs

•Exists attributes•Association•Model’s validations•Class methods•Instance methods

Page 29: Basic RSpec 2

Model specs

For detail, a model spec should include:•Attributeso model attributes should have

•Associationo model association should have

•The model’s create method -> check the validation work?o passed valid attributes => should be valid.o fail validations => should not be valid.

•Class and instance methods perform as expected

Page 30: Basic RSpec 2

Model specs - Example code

Page 31: Basic RSpec 2

code example

Page 32: Basic RSpec 2

Model specs - Example rspec model code

Page 33: Basic RSpec 2

fields, association, validations

Page 34: Basic RSpec 2

The model’s method create

Page 35: Basic RSpec 2

class and instance method

Page 36: Basic RSpec 2

Same and difference

Rspec vs Cucumber

Page 37: Basic RSpec 2

Rspec vs Cucumber

•Both testing frameworks. •Both are used for Acceptance Testing•These are business-case driven Integration

Testso simulate the way a user uses the application, o the way the different parts of your application work

together can be found in a way that unit testing will not find.

same

Page 38: Basic RSpec 2

Rspec vs Cucumber

•RSpec and Cucumber are the business readability factor

difference

CU CU M BER

odraw is that the specification (features) from the test codeoproduct owners can

provide or review without having to dig through code

RSPEC

odescribe a step with a Describe, Context or It block that contains the business specificationoa little easier for

developers to work obut a little harder for

non-technical folks

Page 39: Basic RSpec 2

Rspec vs Cucumber - Example

Page 40: Basic RSpec 2

Integration test with rspec and capybara (and Senelium)

Feature Specs

•Introduce

•Setup env

•Example code

Page 41: Basic RSpec 2

Feature Specs - Introduce

•high-level tests meant to exercise slices of functionality through an application. Drive the application only via its external interface, usually web pages.

•Require the capybara gem, version 2.0.0 or later. Refer to the capybara API for more infos on the methods and matchers

•Feature, scenario, background, given DSL <=>describe, it, before each, let. Alias methods that allow to read more as customer tests and acceptance tests.

Page 42: Basic RSpec 2

Feature Specs - Setup env

First, add Capybara to your Gemfile:

In spec/spec_helper.rb, add two require calls for

Capybara near the top

Capybara’s DSL will be available spec/requests

and spec/integration directory

Page 43: Basic RSpec 2

Feature Specs - Selenium

First, add Capybara to your Gemfile:

In spec/spec_helper.rb, add two require calls for

Capybara near the top

•Run Selenium, just set :js => true

Firefox should automatically

fire up and run with Selenium.

•Capybara.default_driver = :selenium (make

Capybara to all your tests - don’t recommend)

•Using Rack::Test (default driver) and Selenium (JS driver) by setting the :js attribute (faster if use Selenium for tests that actually require JS)

Page 44: Basic RSpec 2

Feature Specs - Sample code

When writing integration tests, try to model the test around an actor (user of the system) and the action they are performing.

Page 45: Basic RSpec 2

Feature Specs - Sample code

Page 46: Basic RSpec 2

code coverage tool

SimpleCov, Rcov

Page 47: Basic RSpec 2

Demo•Model spec•Feature specs

Page 48: Basic RSpec 2

Thank You!