Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yury Kaliada <fut.wrk@gmail.com>
- Loading branch information
Showing
17 changed files
with
2,431 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
courses-ruby-testing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2.2.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'minitest' | ||
gem 'rspec' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
diff-lcs (1.2.5) | ||
minitest (5.6.1) | ||
rspec (3.2.0) | ||
rspec-core (~> 3.2.0) | ||
rspec-expectations (~> 3.2.0) | ||
rspec-mocks (~> 3.2.0) | ||
rspec-core (3.2.3) | ||
rspec-support (~> 3.2.0) | ||
rspec-expectations (3.2.1) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.2.0) | ||
rspec-mocks (3.2.1) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.2.0) | ||
rspec-support (3.2.2) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
minitest | ||
rspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require_relative "train" | ||
require "minitest/autorun" | ||
require "minitest/pride" | ||
|
||
describe Train do | ||
describe "when created without params" do | ||
before do | ||
@train = Train.new | ||
end | ||
|
||
it "must have default name" do | ||
@train.name.must_equal "Uber train" | ||
end | ||
|
||
it "must have default size" do | ||
@train.size.must_equal 10 | ||
end | ||
end | ||
|
||
describe "when created with specific params" do | ||
before do | ||
@train = Train.new 'Express', 1822 | ||
end | ||
|
||
it "must have default name" do | ||
@train.name.must_equal 'Express' | ||
end | ||
|
||
it "must have default size" do | ||
@train.size.must_equal 1822 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Train | ||
ON_HOLD = :on_hold | ||
RUNNING = :running | ||
|
||
attr_reader :name, :size, :state | ||
|
||
def initialize(name='Uber train', size=10) | ||
@name = name | ||
@size = size | ||
@state = ON_HOLD | ||
end | ||
|
||
def run | ||
@state = RUNNING | ||
end | ||
|
||
def stop | ||
@state = ON_HOLD | ||
end | ||
|
||
def get_to_next_station(distance) | ||
sleep 0.00002 * distance * Math.log(distance) | ||
end | ||
|
||
def will_arrive_by_the_end_of_the_day?(tripDuration) | ||
Time.now + tripDuration <= ::Date.today + 1.day | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
# Routing | ||
|
||
``` | ||
get '/patients/:id', to: 'patients#show' | ||
get 'profile', to: :show | ||
get '/patients/:id', to: 'patients#show', as: 'patient' | ||
<%= link_to 'Patient Record', patient_path(@patient) %> | ||
``` | ||
|
||
## Links for resources | ||
``` | ||
resources :photos | ||
GET /photos index display a list of all photos | ||
GET /photos/new new return an HTML form for creating a new photo | ||
POST /photos create create a new photo | ||
GET /photos/:id show display a specific photo | ||
GET /photos/:id/edit edit return an HTML form for editing a photo | ||
PATCH/PUT /photos/:id update update a specific photo | ||
DELETE /photos/:id destroy delete a specific photo | ||
``` | ||
|
||
|
||
## Links for resource | ||
``` | ||
resource :geocoder | ||
GET /geocoder/new new return an HTML form for creating the geocoder | ||
POST /geocoder create create the new geocoder | ||
GET /geocoder show display the one and only geocoder resource | ||
GET /geocoder/edit edit return an HTML form for editing the geocoder | ||
PATCH/PUT /geocoder update update the one and only geocoder resource | ||
DELETE /geocoder destroy delete the geocoder resource | ||
``` | ||
|
||
## Controller Namespaces and Routing | ||
``` | ||
namespace :admin do | ||
resources :posts, :comments | ||
end | ||
GET /admin/posts index admin_posts_path | ||
GET /admin/posts/new new new_admin_post_path | ||
POST /admin/posts create admin_posts_path | ||
GET /admin/posts/:id show admin_post_path(:id) | ||
GET /admin/posts/:id/edit edit edit_admin_post_path(:id) | ||
PATCH/PUT /admin/posts/:id update admin_post_path(:id) | ||
DELETE /admin/posts/:id destroy admin_post_path(:id) | ||
``` | ||
|
||
``` | ||
scope '/admin' do | ||
resources :posts, :comments | ||
end | ||
GET /admin/posts index posts_path | ||
GET /admin/posts/new new new_post_path | ||
POST /admin/posts create posts_path | ||
GET /admin/posts/:id show post_path(:id) | ||
GET /admin/posts/:id/edit edit edit_post_path(:id) | ||
PATCH/PUT /admin/posts/:id update post_path(:id) | ||
DELETE /admin/posts/:id destroy post_path(:id) | ||
``` | ||
|
||
## Nested Resources | ||
``` | ||
class Magazine < ActiveRecord::Base | ||
has_many :ads | ||
end | ||
class Ad < ActiveRecord::Base | ||
belongs_to :magazine | ||
end | ||
resources :magazines do | ||
resources :ads | ||
end | ||
GET /magazines/:magazine_id/ads index display a list of all ads for a specific magazine | ||
GET /magazines/:magazine_id/ads/new new return an HTML form for creating a new ad belonging to a specific magazine | ||
POST /magazines/:magazine_id/ads create create a new ad belonging to a specific magazine | ||
GET /magazines/:magazine_id/ads/:id show display a specific ad belonging to a specific magazine | ||
GET /magazines/:magazine_id/ads/:id/edit edit return an HTML form for editing an ad belonging to a specific magazine | ||
PATCH/PUT /magazines/:magazine_id/ads/:id update update a specific ad belonging to a specific magazine | ||
DELETE /magazines/:magazine_id/ads/:id destroy delete a specific ad belonging to a specific magazine | ||
resources :publishers do | ||
resources :magazines do | ||
resources :photos | ||
end | ||
end | ||
``` | ||
|
||
## Shallow nesting | ||
``` | ||
resources :posts do | ||
resources :comments, only: [:index, :new, :create] | ||
end | ||
resources :comments, only: [:show, :edit, :update, :destroy] | ||
resources :posts do | ||
resources :comments, shallow: true | ||
end | ||
resources :posts, shallow: true do | ||
resources :comments | ||
resources :quotes | ||
resources :drafts | ||
end | ||
shallow do | ||
resources :posts do | ||
resources :comments | ||
resources :quotes | ||
resources :drafts | ||
end | ||
end | ||
scope shallow_path: "sekret" do | ||
resources :posts do | ||
resources :comments, shallow: true | ||
end | ||
end | ||
``` | ||
|
||
## Routing concerns | ||
``` | ||
concern :commentable do | ||
resources :comments | ||
end | ||
concern :image_attachable do | ||
resources :images, only: :index | ||
end | ||
resources :messages, concerns: :commentable | ||
resources :posts, concerns: [:commentable, :image_attachable] | ||
resources :messages do | ||
resources :comments | ||
end | ||
resources :posts do | ||
resources :comments | ||
resources :images, only: :index | ||
end | ||
namespace :posts do | ||
concerns :commentable | ||
end | ||
``` | ||
|
||
|
||
## Links | ||
``` | ||
<%= link_to 'Ad details', magazine_ad_path(@magazine, @ad) %> | ||
<%= link_to 'Ad details', url_for([@magazine, @ad]) %> | ||
<%= link_to 'Ad details', [@magazine, @ad] %> | ||
<%= link_to 'Magazine details', @magazine %> | ||
<%= link_to 'Edit Ad', [:edit, @magazine, @ad] %> | ||
``` | ||
|
||
## Adding More RESTful Actions | ||
``` | ||
resources :photos do | ||
member do | ||
get 'preview' | ||
end | ||
end | ||
resources :photos do | ||
get 'preview', on: :member | ||
end | ||
resources :photos do | ||
collection do | ||
get 'search' | ||
end | ||
end | ||
resources :photos do | ||
get 'search', on: :collection | ||
end | ||
``` | ||
|
||
|
||
``` | ||
get ':controller(/:action(/:id))' | ||
get ':controller(/:action(/:id))', controller: /admin\/[^\/]+/ | ||
get ':controller/:action/:id/with_user/:user_id' | ||
``` | ||
|
||
## HTTP Verb Constraints | ||
``` | ||
match 'photos', to: 'photos#show', via: [:get, :post] | ||
match 'photos', to: 'photos#show', via: :all | ||
``` | ||
|
||
## Segment Constraints | ||
``` | ||
get 'photos/:id', to: 'photos#show', constraints: { id: /[A-Z]\d{5}/ } | ||
get '/stories', to: redirect('/posts') | ||
get '/stories/:name', to: redirect('/posts/%{name}') | ||
get '/stories/:name', to: redirect {|params, req| "/posts/#{params[:name].pluralize}" } | ||
root to: 'pages#main' | ||
root 'pages#main' # shortcut for the above | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Ruby testing | ||
|
||
|
||
``` | ||
rails g model category name | ||
rails g model beer name category:references quantity:integer | ||
rails g model beer_action action:string beer:references quantity:integer | ||
rails g controller beers | ||
``` | ||
|
||
``` | ||
Category.create(name: 'Light'); Category.create(name: 'Dark') | ||
100.times { |i| Beer.create(name: "Beer #{i}", category: Category.all.sample, quantity: rand(1..20)) } | ||
``` | ||
|
||
* show some basic UI | ||
* review tests | ||
* explain fixtures | ||
* switch to minitest | ||
|
||
config.autoload_paths += Dir["#{config.root}/lib/" | ||
|
||
|
||
* rspec explanation | ||
* testing models | ||
* simple beer creating test | ||
* explaining database cleaner | ||
|
||
|
||
http://loudcoding.com/posts/quick-tutorial-starting-with-cucumber-and-capybara-bdd-on-rails-project/ | ||
|
||
|
||
``` | ||
1 Given(/^I bought (\d+) (\w+) beers$/) do |number, category| | ||
2 category = Category.find_or_create_by(name: category) | ||
3 number.to_i.times { |n| Beer.create(name: '', category: category) } | ||
4 end | ||
5 | ||
6 When(/^Open Home page$/) do | ||
7 visit root_path | ||
8 end | ||
9 | ||
10 Then(/^I should see (\d+) beers in list$/) do |arg1| | ||
11 page.has_css?('.table tbody tr:nth-child(2)') | ||
12 end | ||
``` | ||
|
||
|
||
``` | ||
1 Feature: Beer list | ||
2 Scenario: Open beer list | ||
3 Given I bought 2 Light beers | ||
4 When Open Home page | ||
5 Then I should see 2 beers in list | ||
``` | ||
|
Oops, something went wrong.