相关疑难解决方法(0)

匹配中的多个Expect语句的自定义断言消息

我在Rspec中编写了一个自定义match方法,该对象将对象与哈希匹配。我想做的是为的每一行设置自定义失败消息expect。

describe "/cars" do
  car = FactoryGirl.create(:car, name: 'Alpha')
  describe car do
    it "displays single items" do
      get cars_path
      parsed_response = JSON.parse(response.body)
      record_hash = parsed_response['cars'][0]
      is_expected.to be_a_car_match_of(record_hash)
    end
  end
end

RSpec::Matchers.define :be_a_car_match_of do |hash|
  match do |car|
    expect(car.id).to    eq(hash['id'])
    expect(car.name).to  eq(hash['name'])
  end
  failure_message do |car|
    "expected that #{car} would be a match of #{hash}"
  end
end
Run Code Online (Sandbox Code Playgroud)

所以我想要的是以下内容:

RSpec::Matchers.define :be_a_car_match_of do |hash|
  match do |car|
    expect(car.id).to    eq(hash['id'])    'ids did not match'
    expect(car.name).to  eq(hash['name'])  'names did not match'
  end
end …
Run Code Online (Sandbox Code Playgroud)

ruby rspec

5
推荐指数
1
解决办法
885
查看次数

标签 统计

rspec ×1

ruby ×1