使用rspec 开始测试


rails new project

bundle add rspec

Test-Driven Development (TDD) with RSpec

rspec -init
这个命令给我们创建测试的一个起点. 一个.rspec  文件包含一个 require spec/spec_helper.rb

.rspec  -> spec/spec_helper.rb

create  a new rspec test

#human_spec.rb
RSpec.describe Human "#greet" do 
  it "should greet sucessfully" do
    human = Human.new
    expect(human.greet).to eq "Hello, world!"
  end
end

测试文件不同的地方在于通过 _spec.rb结尾.这是要测试的Human类的实例的greet方法.

首先是 Rspec.describe, 描述我们想测试的类 Human 和这个Human的方法 #greet

这个时候运行rspec 会报错,  这是测试驱动开发的精髓...
先写测试,报错,然后你写代码去消除这个错误.

#human.rb
class Human
  def greet
    "Hello, world!"
  end
end

然后在spec/human_spec.rb里面要去包含这个human.rb的类文件
require 'your file path/human.rb'


rspec
.

Finished in 0.0077 seconds (files took 0.15475 seconds to load)
1 example, 0 failures


如何只测试某个文件/某些文件/文件里面的某部分呢?

$ rspec spec/models/your_spec.rb
$ rspec spec/jobs
$ rspec spec/jobs  spec/services
#指定某个文件里面的某一行开始的那部分的测试,
$ rspec spec/person_spec.rb:21

# 这一行可以describe, context,it or expect
# 如果是空行,那么就从头 it 开始,如果空行在所有测试的上面,就测全部.
$ rspec spec/person_spec.rb:[1:3]
# [x:y]格式 x是第x个describe, y是里面的第y个content
$ rspec spec/person_spec.rb:[1:1, 1:3]

#

还可以搜索describe里面的内容,并执行对应的测试例子.
$ rspec spec/person_spec.rb  -e "greet"

-e 这个flag是 --example的short, short  for example

这里的搜索区分大小写的. It is worth noting that this is sensitive.
Running $ rspec person_spec.rb -e "greet" -fd in on our spec file will return

Run options: include {:full_description=>/greet/}

Person
culture
should be able to greet in local language

Finished in 0.00088 seconds (files took 0.09423 seconds to load)
1 example, 0 failures


通过配置精准定位到某个测试. 使用 focus,  it=> fit, describe=> fdescribe
...
RSpec.configure do |config|
config.filter_run_when_matching focus: true
end

you can add f to describe, context or it so it becomes fdescribe, fcontext or fit and RSpec will “focus” on that example/group. 


fit 'should tell height' do
  expect(@person.height).to eq(160)
end

is equivalent to. 上面等同于

it 'should tell height', focus: true do
expect(@person.height).to eq(160)
end

三\如何只测试失败的的测试.
$ rspec file_name_spec.rb --only-failures
如果你只想测试之前失败的test的,那么你就要跟踪哪些是上次失败的tests.
那就需要增加一个配置告诉rspec去存储哪些tests是失败的/通过的.

RSpec.configure do |config|
config.example_status_persistence_file_path = 'some_file.txt'
end

如果你用github,那么记得把some_file.txt加入到.gitignore
需要先把全部的examples运行一遍后,再来执行 
rspec person_spec.rb --only-failures

在上面的基础上, 如果错误太多,只run一个失败的test怎么弄?
rspec person_spec.rb --next-failure
一次只run一个错误

四\通过标签去运行对应的test
你可以tag examples/groups通过一个hash, RSpec称她为 "metadata" 元数据?
比如:force: true 就是一个metadata.
你可以用arbitrary 的key/values ,随你怎么定义key/values
比如想标注那些重要的tests.
我们加一个  important: true


    context 'identity', important: true do # 👈
      it 'should tell first name' do
        expect(@person.first_name).to eq('Emmanuel')
      end

      it 'should tell last name'  do
        expect(@person.last_name).to eq('Hayford')
      end
    end

    context 'physical features', important: false do # 👈
      it 'should be able to tell height' do
        expect(@person.height).to eq(160)
      end

      it 'should be able to tell eye colour' do
        expect(@person.eye_colour).to eq('grey')
      end
    end



rspec person_spec.rb --tag important
那就只会运行 important: true的tests.

通过上面的选择性测试, 这样会节约我们很多测试运行的时间. 让我们做点别的事. 建议在开发中使用这些方法挑选tests去执行,提升开发速度.
阅读量: 207
发布于:
修改于: