RSpec自定义matcher

链接 https://relishapp.com/rspec/rspec-expectations/v/3-4/docs/custom-matchers/define-a-custom-matcher#define-aliases-for-your-matcher

 

 1 require 'rspec/expectations'
 2 class String
 3   def words
 4     split('')
 5   end
 6 end
 7 
 8 RSpec::Matchers.define :have_5_words do
 9   match do |thing|
10      thing.words.length == 5
11   end
12 end
13 
14 RSpec.describe String do
15   it { expect("hello").to have_5_words }
16 end

 

 

 

2.0版本 链接 https://relishapp.com/rspec/rspec-expectations/v/2-0/docs/matchers/have-n-items-matcher

 1 class String
 2   def words
 3     split(' ')
 4   end
 5 end
 6 
 7  describe String.new('hello') do
 8       it {
 9         should have(5).words }
10     end

 

你可能感兴趣的:(RSpec自定义matcher)