我有一个这样的测试:

it "should not indicate backwards jumps if the checker position is not a king" do
    board = Board.new
    game_board = board.create_test_board
    board.add_checker(game_board, :red, 3, 3)
    x_coord = 3
    y_coord = 3
    jump_locations = {}
    jump_locations["upper_left"]  = true 
    jump_locations["upper_right"] = false 
    jump_locations["lower_left"]  = false
    jump_locations["lower_right"] = true
    adjusted_jump_locations = @bs.adjust_jump_locations_if_not_king(game_board, x_coord, y_coord, jump_locations)
    adjusted_jump_locations["upper_left"].should == true 
    adjusted_jump_locations["upper_right"].should == false 
    adjusted_jump_locations["lower_left"].should == false
    adjusted_jump_locations["lower_right"].should == false
  end 

我知道这很冗长.有没有更简洁的方式来表达我的期望?我看了这些文件,但看不到哪里可以压缩我的期望.谢谢

推荐答案

它也适用于哈希:

expect(jump_locations).to include(
  "upper_left"  => true,
  "upper_right" => false,
  "lower_left"  => false,
  "lower_right" => true
)

资料来源:

Ruby相关问答推荐

获取用于传递参数的变量名称

BasicObject 的 singleton_class 的超类如何以及为什么是 Ruby 中的 Class 类?

Rack::Request - 如何获取所有标头?

ruby 中的魔术注释(#Encoding: utf-8)是如何工作的?

我可以在 Ruby 的 heredoc 中访问变量吗?

有没有办法通过哈希初始化对象?

如何通过反射获取 Ruby 的 Module 类定义的常量?

Ruby 中的关联数组

线程安全:Ruby 中的类变量

通过 x 个字符在 Ruby 中获取子字符串

如何在数组中找到出现次数最多的项目

如何在 Ruby 的 IRB 中启用自动完成

如何理解 strptime 与 strftime

项目目录的相对路径

如何在 Rails 3 迁移中描述枚举列?

Ruby总是Round Up

class << self vs self.method with Ruby:什么更好?

类变量上的 attr_accessor

在 Ruby 中创建二维数组和访问子数组

Rubocop 25 线块大小和 RSpec 测试