Rspec: Should be (this or that)

后端 未结 6 1177
無奈伤痛
無奈伤痛 2020-12-16 09:38

What would be the best way to write the rspec in a situation where either of two (or more) outcomes are acceptable?

Here\'s an example of what I want to do. This is

相关标签:
6条回答
  • 2020-12-16 10:01

    I'd probably write something like this:

    it "should be heads or tails" do
      ["heads", "tails"].should include flip_coin
    end
    
    0 讨论(0)
  • 2020-12-16 10:10

    ActiveSupport provides Object#in? method. You can combine it with RSpec and simply use the following:

    flip_coin.should be_in(["heads", "tails"])
    

    Or with new Rspec 3 syntax:

    expect(flip_coin).to be_in(["heads", "tails"])
    
    0 讨论(0)
  • 2020-12-16 10:16

    if applied or with be matcher

    expect(flip_coin).to eq('heads').or(be == 'tails')
    
    0 讨论(0)
  • 2020-12-16 10:17

    You can solve this by flipping the comparison:

    expect(['head','tails']).to include(flip_coin)

    0 讨论(0)
  • 2020-12-16 10:19

    I know this is old but in I ran into this on RSpec 3.4, there is an or keyword now. So this is valid:

    expect(flip_coin).to eq('heads').or(eq('tails'))
    
    0 讨论(0)
  • 2020-12-16 10:23

    Another way of writing it with the expectation on the right of the should:

    it 'should be heads or tails' do
      flip_coin.should satisfy{|s| ['heads', 'tails'].include?(s)}
    end
    
    0 讨论(0)
提交回复
热议问题