Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. # RSPEC CAPYBARA PSQL ROR CONFIG
  2.  
  3. A Ruby on Rails configuration for RSpec Capybara testing with a postgres database.
  4.  
  5. ## Create app
  6.  
  7. ### Postgres, and skip test files:
  8. ```
  9. rails new capyspecpsql -d postgres -T
  10. ```
  11.  
  12. ## Modify files:
  13. * `Gemfile`
  14. * `.rspec`
  15. * `spec_helper.rb`
  16. * `rails_helper.rb`
  17.  
  18. ### Gemfile
  19.  
  20. ```
  21. group :development, :test do
  22. gem 'rspec-rails', '~> 3.8', '>= 3.8.2'
  23. gem 'faker', '~> 1.9', '>= 1.9.3'
  24. gem 'factory_bot_rails', '~> 5.0', '>= 5.0.2'
  25. end
  26.  
  27. group :development do
  28. # rest ommitted
  29. gem 'spring-commands-rspec', '~> 1.0', '>= 1.0.4' # faster app boot time
  30. end
  31.  
  32. group :test do
  33. gem 'database_cleaner', '~> 1.7'
  34. gem 'capybara', '~> 3.22'
  35. end
  36. ```
  37.  
  38. ### .rspec
  39. ```
  40. --color
  41. --require spec_helper
  42. --format doc
  43. ```
  44.  
  45. ### spec_helper.rb
  46. ```
  47. config.order = :random # runs test cases in random order
  48. Kernel.srand config.seed # to replicate exact test order
  49. ```
  50. ### rails_helper.rb
  51.  
  52. ```
  53. require 'database_cleaner' # eliminates dependencies by cleans database between specs
  54. require 'capybara/rspec'
  55. ```
  56.  
  57. ```
  58. RSpec.configure do |config|
  59. # truncation prefered strategy with JS test cases
  60. config.use_transaction_fixtures = false
  61. # use truncation to clean before each suite:
  62. config.before(:suite) do
  63. DatabaseCleaner.clean_with(:truncation)
  64. end
  65. # use truncation before each test for JS, transaction otherwise
  66. config.before(:each) do |example|
  67. DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction
  68. end
  69. # clean db after each spec
  70. config.after(:each) do
  71. DatabaseCleaner.clean
  72. end
  73. end
  74. ```
  75.  
  76. ## Troubleshooting
  77.  
  78. If the following warning occurs after executing `rspec .`
  79. > You must use Bundler 2 or greater with this lockfile.
  80.  
  81. Try the following command and run `rspec .` again:
  82. ` gem install bundler && bundler update --bundler`
  83.  
  84. ## Modifying `config/application.rb`
  85. ```
  86. config.generators do |g|
  87. g.test_framework :rspec,
  88. :fixtures => true, # generate a fixture for each model
  89. :view_specs => false,
  90. :helper_specs => false,
  91. # omits spec file for routes.rb:
  92. :routing_specs => false, # switch if app grows
  93. :controller_specs => true,
  94. :request_specs => true
  95. # generate factories instead of fixtures with target save dir
  96. g.fixture_replacement :factory_bot, :dir => "spec/factories"
  97. end
  98. ```
  99.  
  100. ## Examples
  101.  
  102. ### Quick version
  103.  
  104. ```
  105. $ rails g scaffold Album title:string
  106. $ rails db:migrate RAILS_ENV=test
  107. $ bundle exec spring binstub rspec
  108. ```
  109.  
  110. * `album_spec.rb`
  111. ```
  112. require 'rails_helper'
  113.  
  114. RSpec.describe Album do
  115. #let(:album) {Album.new}
  116. subject { Album.new }
  117.  
  118. it 'is not valid without a title' do
  119. expect(subject).not_to be_valid
  120. end
  121.  
  122. it 'is not valid without a title longer than 100 chars' do
  123. subject.title = 'a' * 101
  124. expect(subject).not_to be_valid
  125. end
  126.  
  127. it 'is valid with proper data' do
  128. subject.title = 'a' * 50
  129. expect(subject).to be_valid
  130. end
  131. end
  132. ```
  133.  
  134. * `album.rb`
  135. ```
  136. class Album < ApplicationRecord
  137. validates :title, presence: true,
  138. length: { maximum: 100 }
  139. end
  140. ```
  141.  
  142. #### Run tests
  143. `$ rspec .`
  144.  
  145. ### Longer version
  146. ```
  147. $ rails g model Album title:string
  148. $ rails db:migrate RAILS_ENV=test
  149. $ bundle exec spring binstub rspec
  150. ```
  151.  
  152. * `album_spec.rb`
  153. ```
  154. require 'rails_helper'
  155.  
  156. RSpec.describe Album do
  157. #let(:album) {Album.new}
  158. subject { Album.new }
  159.  
  160. it 'is not valid without a title' do
  161. expect(subject).not_to be_valid
  162. end
  163.  
  164. it 'is not valid without a title longer than 100 chars' do
  165. subject.title = 'a' * 101
  166. expect(subject).not_to be_valid
  167. end
  168.  
  169. it 'is valid with proper data' do
  170. subject.title = 'a' * 50
  171. expect(subject).to be_valid
  172. end
  173. end
  174. ```
  175.  
  176. * `album.rb`
  177. ```
  178. class Album < ApplicationRecord
  179. validates :title, presence: true,
  180. length: { maximum: 100 }
  181. end
  182. ```
  183.  
  184. ```
  185. $ rails db:migrate RAILS_ENV=test && rspec .
  186. ```
  187.  
  188. * `index_spec.rb`
  189.  
  190. ```
  191. require 'rails_helper'
  192.  
  193. RSpec.feature 'Albums list' do # RSpec.describe '', type: :feature
  194. scenario 'unauthenticated user' do # alias for it method
  195. visit albums_path
  196. within '#content' do
  197. expect(find('h1')).to have_content('Albums')
  198. end
  199. # expect(find('#content h1')).to have_content('Albums')
  200. # to use xpath instead of css do: find(:xpath, 'foo')
  201. end
  202. end
  203. ```
  204.  
  205. * `albums_controller.rb`
  206.  
  207. ```
  208. class AlbumsController < ApplicationController
  209. def index
  210. end
  211. end
  212. ```
  213.  
  214. * `app/views/albums/index.html.erb`
  215. ```
  216. <h1>Albums</h1>
  217. ```
  218.  
  219. * `app/views/layouts/application.html.erb`
  220.  
  221. ```
  222. <!DOCTYPE html>
  223. <html>
  224. <head>
  225. <meta charset="utf-8">
  226. <title>Rorspec</title>
  227. <%= csrf_meta_tags %>
  228. <%= csp_meta_tag %>
  229.  
  230. <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
  231. <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  232. </head>
  233.  
  234. <body>
  235. <div id="content">
  236. <%= yield %>
  237. </div>
  238. </body>
  239. </html>
  240. ```
  241.  
  242. * `routes.rb`
  243. ```
  244. Rails.application.routes.draw do
  245. resources :albums, only: [:index]
  246. root to: 'albums#index'
  247. end
  248. ```
  249.  
  250. `$ rails rspec .`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement