使用RSpec和Capybara进行Rails测试介绍


总结:
多看官方文档,把官方文档当做是首选阅读的文档,其次再去看一些实际的案例。
rspec-rails: 
https://relishapp.com/rspec/rspec-rails/docs

capybara:
https://rubydoc.info/github/teamcapybara/capybara/master
http://teamcapybara.github.io/capybara/

factory_bot  &  factory_bot_rails
https://github.com/thoughtbot/factory_bot
https://github.com/thoughtbot/factory_bot_rails

Cucumber vs RSpec: What are the differences?
Cucumber: Simple, human collaboration.
Cucumber is a tool that supports Behaviour-Driven Development (BDD) - a software development process that aims to enhance software quality and reduce maintenance costs;
RSpec: Behaviour Driven Development for Ruby.


https://rubyyagi.com/intro-rspec-capybara-testing/
Introduction to Rails testing with RSpec and Capybara
使用RSpec和Capybara测试Rails介绍

Testing is heavily emphasized in the Ruby and Rails community,  even the official Ruby on Rails guide has a section dedicated to testing.

A lot of Rails job postings usually requires testing skill (Rspec, Minitest etc) as well:

I didn't understand the importance of testing and used to think writing test was an extra effort (my app already work well when I wrote it, why should I spend extra time to write test for it? ), boy  I was so wrong.

Now I work in an enterprise handling thousands of transaction per day, I would be very afraid to deploy new update if there wasn't  a solid test suite. without a test suite, I could have broke the payment system on a new deploy, which could cost the companies a lot $$$💸!
现在我加入了一个企业,每天要处理上千笔的交易,每次更新我都担心, 如果没有一个solid test suite(测试用例集合),新的发布可能会破坏支付系统,如果那样的话,公司就会损失很多钱。

This is why many companies with good engineering practice would require job candidate to possess skill on writing automated test.
良好工程实践,应聘者job candidate, possess 拥有


Automated test can make us confident in our deploy, ensure there is no regression(倒退回归退化)(ie. new deploy broke old working features), and in contrary,  it can make the development process faster too!

Instead of filling forms and clicking buttons manually every time you make a change(10 seconds), we can tell Capybara to do this for us(in 1-2 seconds).

This article aims to give you a guideline on how to get started on testing , be it on your newly created Rails app or an existing Rails app that already has some users.

We will first write a test to check static content, then move to testing basic CRUD feature.

We will be using Rspec (the testing framework) and Capybara (Browser automation tool to fill in text field, click button)  in this article.

This article  will start from a freshly created Rails app, but you can follow the same instruction on your existing Rails app as well.

Table of contents
  1. Creating the sample Rails app 
  2. Install Rspec-rails and Capybara 
  3. Create a static controller displaying static content 
  4. Write your first test 
  5. Ensure the test actually work by making it fail 
  6. Create a CRUD Scaffold 
  7. Write test for CRUD actions 
  8. No idea on how to start writing automated test? 
  9. References


rails new bookmarker -T

 the -T flag is used to tell Rails not to use MiniTest. 

cd bookmarker
rake db:create


Include these 3 gems in the Gemfile, inside the :development, :test and :test group like this :

# Gemfile

# ...
group :development, :test do
  gem 'rspec-rails'
end

group :test do
  gem 'capybara'
  gem 'webdrivers', '~> 4.0', require: false
end


After installing these gems, we still need to install Rspec into our app using


rails generate rspec:install


This will create a few boilerplate files, and update some configuration in our Rails app.
这将创建一些样板文件,并更新Rails应用程序中的一些配置。

Remember to require the webdrivers gem at the top of spec/rails_helper.rb :
请记住,在spec/rails_helper.rb的顶部需要

 config.include Capybara::DSL  这句可以解决 undefined method `visit' for #<RSpec...的问题。

# spec/rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'


# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  # config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.order = "random"
  # Include path helpers
  config.include Rails.application.routes.url_helpers

  config.include Capybara::DSL

end



rails webpacker:install
rails s



localhost:3000/static/index

阅读量: 614
发布于:
修改于: