rails默认使用的是minitest,默认的模块是Minitest::Test。测试包括model测试,controller测试,view测试,routes测试,helper测试,mailer测试,job测试,database测试,integration测试和system测试。

各种测试的继承关系

#model测试继承
ActiveSupport::TestCase(继承自Minitest::Test)

#mailer测试继承
ActionMailer::TestCase(继承自ActiveSupport::TestCase)

#view测试继承
ActionView::TestCase(继承自ActiveSupport::TestCase)

#system测试继承
ApplicationSystemTestCase(继承自ActionDispatch::IntegrationTest,继承自ActiveSupport::TestCase)

#integration测试继承
ActionDispatch::IntegrationTest(继承ActiveSupport::TestCase)

书写测试用例

#使用model测试书写测试用例,测试方法有两种书写方法,下面依次介绍
#user_test.rb
require 'test_helper'
class UserTest < ActiveSupport::TestCase
  #测试方法的第一种写法,test接受String对象和Block对象
  test "the truth" do
     assert true
  end
  #测试方法的第二种写法,使用定义方法的形式,方法名必须以及“test_*”开头
  def test_the_truth
    assert true
  end
end

测试执行和测试失败

#执行测试文件
rails test #执行全部测试文件
rails test test/models/user_test.rb #执行单个文件的测试

#测试失败问题
f #测试失败,比如assert true, 其实是assert false
e #测试中出现错误,比如变量未定义

可用的assert

assert
assert_not
assert_equal
assert_not_equal
...

测试数据库

RAILS_ENV=test rails c  #test环境下加载数据库
rails db:test:prepare   #若执行了rails migration,测试环境的数据通过这个方法进行更新

#每次执行测试文件之前会将fixture的yml文件更新到test数据库文件中
#users.yml
jayzen:
  name: "user name"
  address: "user address"
#获取fixtrue对象
users(:jayzen)


#可以在yml文件中使用erb文件
<% 1000.times do |n| %>
user_<%=n%>:
  username: <%= "user#{n}" %>
  email: <%= "user#{n}@example.com" %>
<% end %>

model测试

#用例参考上面的案例,继承自ActiveSupport::TestCase
#model测试会生成model测试文件fixture文件
$ bin/rails generate test_unit:model article title:string body:text
create  test/models/article_test.rb
create  test/fixtures/articles.yml

system测试

#系统测试
系统测试用于测试用户与应用的交互,可以在真正的浏览器中运行,也可以在无界面浏览器中运行。系统测试建立在 Capybara之上

#系统测试需要两个gem,rails5默认添加
gem 'capybara' #
gem 'selenium-webdriver'
gem 'minitest', '~> 5.10', '!= 5.10.2' #5.10会有bug, 需要执行bundle update --source minitest


关于capybara的解释
Capybara is an integration testing tool for rack based web applications. 
It simulates how a user would interact with a website

关于selenium-webdriver的解释
WebDriver is a tool for writing automated tests of websites. 
It aims to mimic the behaviour of a real user, and as such interacts with the HTML of the application

#application_system_test_case.rb
require "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
end

#安装chromedriver
brew install chromedriver

#private/etc/hosts添加下面代码
127.0.0.1 localhost

#执行系统测试
rails test:system
rails generate system_test articles
#典型的测试文件articles_test.rb
require "application_system_test_case"
class ArticlesTest < ApplicationSystemTestCase
  test "visiting the index" do
    visit articles_path

    click_on "New Article"

    fill_in "Title", with: "Creating an Article"
    fill_in "Body", with: "Created this article successfully!"

    click_on "Create Article"

    assert_text "Creatg an Article"
  end
end

integration测试

#integration测试
集成测试用于测试应用中不同部分之间的交互,一般用于测试应用中重要的工作流程

#典型测试用例
rails generate integration_test blog_flow

#blog_flow_test.rb
require 'test_helper' 
class BlogFlowTest < ActionDispatch::IntegrationTest
  test "can create an article" do
    get "/articles/new"
    assert_response :success

    post "/articles",
      params: { article: { title: "can create", body: "article successfully." } }
    assert_response :redirect
    follow_redirect!  #重定向后如果还想发送请求,需要调用 follow_redirect!
    assert_response :success
    assert_select "p", "Title:\n  can create"
  end
end

备注

#assert_template

results matching ""

    No results matching ""