Back-end Engineering Articles
I write and talk about backend stuff like Ruby, Ruby On Rails, Databases, Testing, Architecture / Infrastructure / System Design, Cloud, DevOps, Backgroud Jobs, and more...
2020-01-31
## instalation $ rvm use 2.7.1 $ gem install rspec --version 3.10.0 $ rspec --help $ gem install cucumber --version 6.0.0 $ cucumber --help ## Hello RSpec /greeter_scpec.rb class RSpecGreeter def greet "Hello Rspec!" end end describe "Rspec Greeter" do it "should say 'Hello Rspec!' when it receives the greet() message" do greeter = RSpecGreeter.new # Given greeting = greeter.greet # When greeting.should == "Hello Rspec!" #Then end end $ rspec greeter_spec.rb Finished in 0.00393 seconds (files took 0.08857 seconds to load) 1 example, 0 failures ######################################### RSpec and Cucumber with Rails 6 rails --version ## rails 6.0 ## Gemfile group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] ## RSpec ## Testing libraries gem 'rspec-rails', '~> 5.0.0' gem 'cucumber-rails', require: false gem 'database_cleaner' gem 'webrat', '~> 0.7.3' end $ bundle install $ rails g rspec:install $ rails g cucumber:install ## To make sure everything is wired up correctly, run these commands: $ rails db:migrate $ rails db:test:prepare $ rails spec $ rails cucumber ## running first escenarios $ touch features/step_definitions/name_descriptions.feature Feature: So that I can find jobs that fit my skills As a job seeker I want to see accurate and conscince job offers @wip Scenario: Show job offers for all candidates Given a job When I see a good opportunity for me Then the click redirects me to a page to join the job $ rails cucumber:wip
group :development, :test do ... %w[rspec-core rspec-rails rspec-expectations rspec-mocks rspec-support].each do |lib| gem lib, git: "<https://github.com/rspec/#{lib}.git>", branch: 'master' end end group :test do ... gem 'factory_bot_rails' gem 'faker' gem 'database_cleaner' end $ bundle install $ rails g rspec:install
# spec/support/database_cleaner RSpec.configure do |config| config.before(:suite) do DatabaseCleaner.clean_with(:truncation) end config.before(:each) do DatabaseCleaner.strategy = :transaction end config.before(:each, :js => true) do DatabaseCleaner.strategy = :truncation end config.before(:each) do DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end config.before(:all) do DatabaseCleaner.start end config.after(:all) do DatabaseCleaner.clean end end
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
$ bundle exec rspec -f d $ rspec spec/requests/registration_spec.rb $ rspec spec/requests/registration_spec.rb:49 $ rspec spec/requests/registration_spec.rb -f d $ rspec spec/requests/registration_spec.rb:49 -f d
# Gemfile group :development, :test do gem 'rspec-rails', '~> 3.6.0' # leave other gems provided by Rails end # config/databasetest: test: <<: *default database: db/test.sqlite3 test: <<: *default database: projects_test # console $ bin/rails db:create:all $ bin/rails generate rspec:install $ bin/rspec $ bundle exec rspec
module AxiomRailsAPI class Application < Rails::Application ... config.generators do |g| g.test_framework :rspec, fixture: true g.fixture_replacement :factory_bot, dir: 'spec/factories' g.view_specs false g.helper_specs false end end end
$ $bin/rails g rspec:model user ## spec/models/user_spec.rb require 'rails_helper' RSpec.describe User, type: :model do pending "add some examples to (or delete) #{__FILE__}" end ## spec/models/user_spec.rb require 'rails_helper' RSpec.describe User, type: :model do it "is valid with a first name, last name, email, and password" it "is invalid without a first name" it "is invalid without a last name" it "is invalid without an email address" it "is invalid with a duplicate email address" it "returns a user's full name as a string" end
$ rails db:test:prepare RAILS_ENV=test $ rails db:migrate RAILS_ENV=test $ rails g rspec:request corporate_parking/api/api_endpoints