在rspec测试中设置多个期望

时间:2013-12-29 20:20:10

标签: ruby rspec

如何在RSpec中设置多个期望值?例如,在计算机的移动下方是一个样本值,但在四个可能的值中。我想通过测试这四个值中的任何一个。请帮帮忙吗?

我正在尝试测试的代码

def fork_detection_type_1(possible_places, user_sign)
  second_turn_check = possible_places.select { |key, value| value == user_sign }
  @corners = { a1:@a1, a3:@a3, c1:@c1, c3:@c3 }
  kitty_fork_check = second_turn_check.keys & @corners.keys
  if kitty_fork_check == [:a1, :c3] || kitty_fork_check == [:a3, :c1]
    move = { a2:@a2, b3:@b3, c2:@c2, b1:@b1 }.keys.sample
    #declare_computer_move(move)
  else
    #fork_detection_type_2
  end
end

我试图实现的测试效果

 it "should be returning only a sample edge move value when there is a kitty fork type" do
   possible_places = {a1: "X",a2: @a2,a3: @a3,b1: @b1,b2: @b2,b3: @b3,c1: @c1,c2: @c2,c3: "X"}
   user_sign = "X"

   test_case = ComputerLogic.new
   test_case.fork_detection_type_1(possible_places, user_sign).should == :a2 || 
   test_case.fork_detection_type_1(possible_places, user_sign).should == :b3 || 
   test_case.fork_detection_type_1(possible_places, user_sign).should == :c2 || 
   test_case.fork_detection_type_1(possible_places, user_sign).should == :b1
 end

1 个答案:

答案 0 :(得分:0)

您可以通过计算布尔表达式并将结果与​​true进行比较来实现,也可以定义自己的匹配器。

使用expect语法的前一种方法的示例是:

expect([:a2,:b3,:c2,:b1].include?(
  test_case.fork_detection_type_1(possible_places, user_sign)).to be true

后一种方法的一个例子是:

RSpec::Matchers.define :be_one_of do |expected|
  match do |actual|
    expected.include?(actual)
  end
end

具有以下期望:

expect(test_case.fork_detection_type_1(possible_places, user_sign)).to
  be_one_of([:a2,:b3,:c2,:b1])

https://www.relishapp.com/rspec/rspec-expectations/v/2-0/docs/matchers/define-matcher!#define-a-matcher-with-default-messages