Code Monkey home page Code Monkey logo

rspec_learning's Introduction

rails new sample_app –skip-test-unit

Here the –skip-test-unit option to the rails command tells Rails not to generate a test directory associated with the default Test::Unit framework. This is not because we won’t be writing tests; on the contrary, starting in Section 3.2 we will be using an alternate testing framework called RSpec to write a thorough test suite.

group :development, :test do

gem 'sqlite3'
gem 'rspec-rails', '2.11.0'

end group :test do

gem 'capybara', '1.1.2'

end

This includes rspec-rails in development mode so that we have access to RSpec-specific generators, and it includes it in test mode in order to run the tests. We don’t have to install RSpec itself because it is a dependency of rspec-rails and will thus be installed automatically. We also include the Capybara gem, which allows us to simulate a user’s interaction with the sample application using a natural English-like syntax

$ bundle update $ bundle install –without production

Next, we need to configure Rails to use RSpec in place of Test::Unit. This can be accomplished with rails generate rspec:install:

$ rails generate rspec:install

create  .rspec
     create  spec
     create  spec/spec_helper.rb

After this if we create controller

>rails g controller static_pages

create  app/controllers/static_pages_controller.rb
invoke  erb
create    app/views/static_pages
invoke  rspec
create    spec/controllers/static_pages_controller_spec.rb
invoke  helper
create    app/helpers/static_pages_helper.rb
invoke    rspec
create      spec/helpers/static_pages_helper_spec.rb
invoke  assets
invoke    coffee
create      app/assets/javascripts/static_pages.js.coffee
invoke    scss
create      app/assets/stylesheets/static_pages.css.scss

How do I generate specs for existing controllers?

$rails g rspec:controller ControllerName $rails g rspec:model ModelName

Generating a StaticPages controller(if we dont need tests for any controller like static pages)

$ rails generate controller StaticPages home help –no-test-framework

Example on static pages:

rails generate integration_test static_pages
     invoke  rspec
     create    spec/requests/static_pages_spec.rb

     Code to test the contents of the Home page.

spec/requests/static_pages_spec.rb

require ‘spec_helper’

describe “Static pages” do

describe "Home page" do

  it "should have the content 'Sample App'" do
    visit '/static_pages/home'
    page.should have_content('Sample App')
  end
end

end

The code in Listing 3.9 is pure Ruby, but even if you’ve studied Ruby before it probably won’t look very familiar. This is because RSpec uses the general malleability of Ruby to define a domain-specific language (DSL) built just for testing.

To run first test

$ bundle exec rspec spec/requests/static_pages_spec.rb

rspec_learning's People

Contributors

devudilip avatar

Watchers

James Cloos avatar  avatar

Forkers

maruthimg

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.