代码之家  ›  专栏  ›  技术社区  ›  Alexey

如何将消息附加到RSPEC检查?

  •  40
  • Alexey  · 技术社区  · 15 年前

    在rspec中:我可以像在XUnit风格的测试框架中那样将消息附加到检查中吗?怎么用?

    assert_equal value1, value2, "something is wrong"
    
    3 回复  |  直到 11 年前
        1
  •  56
  •   Alexey    11 年前

    should should_not message

    1.should be(2), 'one is not two!'
    

    expect(1).to eq(2), "one is not two!"
    
        2
  •  27
  •   Phil Frost    12 年前

    RSpec documentation

    require 'rspec/expectations'
    
    RSpec::Matchers.define :be_a_multiple_of do |expected|
      match do |actual|
        (actual % expected).zero?
      end
      failure_message_for_should do |actual|
        "expected that #{actual} would be a multiple of #{expected}"
      end
      failure_message_for_should_not do |actual|
        "expected that #{actual} would not be a multiple of #{expected}"
      end
      description do
        "be multiple of #{expected}"
      end
    end
    

    match failure_message_for_should

    match_for_should match_for_should_not

    user.permissions.should be(42), 'user does not have administrative rights'
    

    user.should have_administrative_rights
    

    require 'rspec/expectations'
    
    RSpec::Matchers.define :have_administrative_rights do
      match do |thing|
        thing.permissions == 42
      end
      failure_message_for_should do |actual|
        'user does not have administrative rights'
      end
      failure_message_for_should_not do |actual|
        'user has administrative rights'
      end
    end
    
        3
  •  6
  •   BPH    11 年前

            expect(coder.is_partial?(v)).to eq p, "expected #{v} for #{p}"
    

            expect(coder.is_partial?(v)).to eq(p), "expected #{v} for #{p}"