rspec match_array为空数组失败?

fot*_*nus 5 rspec

在我的match_array一个测试中,我不能使用它来匹配一个空数组.我有以下消息:

Failure/Error: expect(subject.custom_text_locations).to be match_array([])

   expected #<RSpec::Matchers::BuiltIn::MatchArray:105810100> => #<RSpec::Matchers::BuiltIn::MatchArray:0xc9d1168 @expected=[]>
        got #<Array:105810180> => []
Run Code Online (Sandbox Code Playgroud)

这是我的测试:

context 'when there is no custom text locations' do
  subject { create(:service, custom_text_locations: nil) }
  it 'returns empty list' do
    expect(subject.custom_text_locations).to match_array([])
  end 
end 
Run Code Online (Sandbox Code Playgroud)

如果我改变match_array([])be_empty,我的代码工作.另外,正如@PeterAlfvin指出的那样,更改custom_text_locations主题的初始化[]似乎有效.

这是我的方法:

def custom_text_locations
  self[:custom_text_locations] || []
end
Run Code Online (Sandbox Code Playgroud)

问题:我的测试有什么问题?

Pet*_*vin 14

您发布的代码不是生成您发布的错误的代码.

您发布的错误包含错误to be match_array([]),这与to match_array([])您在发布的代码中的错误有很大不同.

  • 你是先知还是什么?:O (2认同)
  • 几乎没有先知,更像是一个盲人.;-)我一直在盯着并错过这种差异的时间超过了我承认的时间! (2认同)