Logo

rspec集成测试的总结

avatar carol 08 Oct 2016

写了大概一个月的rspec测试,每次提交mr都能收到我厂CTO Rain的很多的comments,指出了很多的问题,现在我把这些问题分类好分享给大家参考参考。

首先说一下我写测试的这个项目是使用Gitlab CI(Continuous Integration)进行持续集成,对于CI我之前是一点都不了解的,现在大概知道是干嘛用的,大致就是开发提交代码后,CI会自动跑一遍整个代码,看看有没有冲突或者错误什么的,没问题的代码被合并之后,就会自动部署一个版本,相对来说这样会比较节省时间。测试的项目有三个driver,默认的driver是rack_test,然后我本地调试的时候用的是selenium(方便debug),提交到CI的时候用的是poltergeist。好啦,话不多说,开始正题!

一、文件规范

  • 用rspec写测试,文件起名是有要求的,一定要以XXX_spec.rb这样的格式命名,不然服务器是不会执行那个文件的,本人就犯了这样的错误,还是Rain发现了提醒的我,下次要长记性了!

  • 配置文件要放入单独的support。

  • 注释放在方法名之上。

二、测试数据的创建

写测试少不了数据,这里创建数据我用的是FactoryGirl,关于FactoryGirl我也是第一次写,还不是很熟练,同事写的总结《使用 FactoryGirl 准备测试数据》很不错,欢迎围观~

  • 同一个Model有不同初始状态的用例时,要使用FactoryGirl的trait helper来减少重复。
FactoryGirl.define do
  factory :manager do
    sequence(:email) { |n| "test#{n}@example.com" }
    password 12345678
    role :test01

    trait :test02 do
      email "test02@example.com"
      username "test02"
      role :test02
    end

    trait :test03 do
      email "test03@example.com"
      username "test03"
      role :test03
    end
  end
end
  • 单独的model,应该使用单独的factory文件。
  • 数据准备必需用given。
feature "manager" do
  given! (:super_manager){ 
    create(:manager, :super_manager)
  }
end
  • given里面只能发一个数据准备。
  • 测试内容不要用变量,要保证测试是稳定,要用明文内容,例如 “1234”,应该在构造测试数据时指定。
# 设定好登录验证码为123456
given! (:member_approved){ create :member, :approved, :with_sms_auth_code, sms_auth_code: 123456 }
# 不建议这样写
 fill_in "verification_code", with: member_approved.sms_auth_code

# 建议这样写
 fill_in "verification_code", with: "123456"
  • 时间必需用lambda,否则会停止在定义时。
created_at { 31.days.ago }

三、测试代码尽量简洁明了

Rain说,好的测试代码是不懂技术的人也能看得懂,所以最好把复杂的代码封装成helper,保持测试代码的简洁。本项目用的是capybara,而capybara本身提供的很多封装好的helper(传送门),直接调用就好了。

  • 多用helper。
  • 使用明文内容断言。
expect(page).to have_content "Hello"
  • 简单方法掉建议取掉”()”,一来减少输入,二来增加可读性。
click_link("编辑")
# 改为
click_link "编辑"
  • 复杂的代码记得加注释。
  • 测试步骤比较繁琐的话可以定义个简单DSL step helper,如下:
# a simple DSL for separating test codes
def step(desc, &block)
  yield
end
  • 需要共享step的话,可以使用 rspec自带的shared examples或者第三方的rspec-steps(传送门)。

给大家看看本人进步后写的代码例子:

require 'rails_helper'
feature "content manager"  do
  given! (:content_manager){ create :manager, :content_manager }
  given! (:member){ create :member }
  given! (:member_approved){ create :member, :approved }

  background do
    visit '/admin'
    sign_in "content_manager@example.com", "11111111"
  end

  scenario "send notification by sms", js: true do
    click_link "推送管理"
    click_link "新建推送"

    step "fill in description" do
      fill_in "notification[content]", with: "知名大咖新发布了三篇文章"
      click_link "下一步"
    end

    step "select members" do
      click_button "选择会员"
      check "select-all"
      click_button "添加"
      expect(page).to have_content "amy"
      click_link "下一步"
    end

    step "submit and check" do
      click_link "提交"
      click_link "历史推送"
      expect(page).to have_content "成功"
    end
  end
end

总结

这段时间写的测试,初步认识了CI是怎么工作的,学会使用FactoryGirl做一些简单的数据创建,比上一次写集成测试进步了一点,代码规范被Rain纠正后好了很多,一些简单的测试也相对比较熟练了,再接再厉。

最后感谢指导老师Rain!

Tags
Test
RSpec
FactoryGirl
Contattaci