Basic RSpec 2

Post on 12-May-2015

1080 Views

Category:

Education

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Authors: Triet Le - Truc Nguyen

Transcript

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

Test Driven Development

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

•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

Behavior Driven Development

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)

RSpec?

RSpec

•Rspec is unit-testing framework for Ruby programming language

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

How

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

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

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)

For Example:

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

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.

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

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

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!

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

Rspec-rails

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

•Recommended oFactory-girloGuard-spec oSpork o SimpleCov

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

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

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']

What need to test in model?

Model Specs

Model specs

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

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

Model specs - Example code

code example

Model specs - Example rspec model code

fields, association, validations

The model’s method create

class and instance method

Same and difference

Rspec vs Cucumber

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

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

Rspec vs Cucumber - Example

Integration test with rspec and capybara (and Senelium)

Feature Specs

•Introduce

•Setup env

•Example code

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.

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

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)

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.

Feature Specs - Sample code

code coverage tool

SimpleCov, Rcov

Demo•Model spec•Feature specs

Thank You!

top related