/mostly_static_pages_5.0

Mostly Static Pages from ruby on Rails Tutorial by M. Hartl

Primary LanguageRuby

Ruby on Rails Tutorial sample application

This is the sample application for Ruby on Rails Tutorial: Learn Web Development with Rails by Michael Hartl.

Getting started

To get started with the app, clone the repo and then install the needed gems:

$ bundle install --without production

Next, migrate the database:

$ rails db:migrate

Finally, run the test suite to verify that everything is working correctly:

$ rails test

If the test suite passes, you'll be ready to run the app in a local server:

$ rails server

Mostly Static Pages

rails new mostly_static_pages
cd mostly_static_pages
git init
# update README.md
git add .
git commit -m "initial commit"
rails generate controller StaticPages home help
    create  app/controllers/static_pages_controller.rb
     route  get 'static_pages/help'
     route  get 'static_pages/home'
    invoke  erb
    create    app/views/static_pages
    create    app/views/static_pages/home.html.erb
    create    app/views/static_pages/help.html.erb
    invoke  test_unit
    create    test/controllers/static_pages_controller_test.rb

config/routes.rb

Rails.application.routes.draw do
  get 'home', to: 'static_pages#home'
  get 'help', to: 'static_pages#help'
  root 'static_pages#home'
end

Getting started with testing – MiniTest

rails routes
Prefix Verb URI Pattern     Controller#Action
  home GET  /home(.:format) static_pages#home
  help GET  /help(.:format) static_pages#help
  root GET  /               static_pages#home

test/controllers/static_pages_controller_test.rb

class StaticPagesControllerTest < ActionDispatch::IntegrationTest
  test 'should get home' do
    get home_url
    assert_response :success
  end

  test 'should get help' do
    get help_url
    assert_response :success
  end

  test 'should get root page' do
    get root_url
    assert_response :success
  end
end
rails test

Getting started with testing – RSpec

rm -rf test

Gemfile

group :development, :test do
  gem 'rspec-rails', '~> 3.5'
  gem 'rails-controller-testing'
  gem 'capybara'
end
rails g rspec:install
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb
rails generate controller StaticPages home help
        invoke  rspec
        create    spec/controllers/static_pages_controller_spec.rb
        create    spec/views/static_pages
        create    spec/views/static_pages/home.html.erb_spec.rb
        create    spec/views/static_pages/help.html.erb_spec.rb
        invoke  helper
rm -rf spec/helpers/
rails db:migrate

Clean up routes.rb.

spec/
└── views
    └── static_pages
        ├── help.html.erb_spec.rb
        └── home.html.erb_spec.rb

Takie same poprawki w help.html.erb.

RSpec.describe "static_pages/home.html.erb", type: :view do
  # pending "add some examples to (or delete) #{__FILE__}"
  it "displays string StaticPages#home" do
    render
    expect(rendered).to include("StaticPages#home")
  end
end
spec/
├── controllers
│   └── static_pages_controller_spec.rb
RSpec.describe StaticPagesController, type: :controller do
  describe "GET #home" do
    it "returns http success" do
      get :home
      expect(response).to have_http_status(:success)
    end
    it "renders the home template" do
      get :home
      expect(response).to render_template("home")
    end
  end

  describe "GET #help" do
    it "returns http success" do
      get :help
      expect(response).to have_http_status(:success)
    end
  end
end
rspec -fd

Getting started with Capybara

Dopisujemy do pliku rails_helper.rb w bloku config:

# RSpec.configure do |config|
  config.include Capybara::DSL
RSpec.describe "Static Pages", type: :request do
  it 'home page displays the string StaticPages#home' do
    visit '/'
    expect(page).to have_content('StaticPages#home')
    expect(page).to have_selector('h1', text: 'StaticPages#home')
  end
end
rspec -fd

Więcej informacji – Using Capybara with RSpec